Second installment.
Teaches:
coding conventions
use of combo box
Select Case
combining strings
The idea is to make a very simple program that will display like a cricket scorecard when you enter the information required.
First start a new project and add 4 text boxes, a combo box, a button and 5 labels like so:
Use the properties menu to change the text on the labels:
Like so:
Click on one of the text boxes and go to properties again. This time change the
name not the text:
The names we are going to use are as follows:
for the bowler box - txtbowler
for the batsman box - txtbatsman
for the how out box - cbohowout
for the fielder box - txtfielder
for the runs box - txtruns
for the display button - cmddisplay
These are all vb.net coding conventions. See
here for more information.
Your drop down box (on the properties menu) should look like this:
Next we're going to add some items into the combo box. Click onto the box and on the properties menu, click on the box by collection.
A box for you to type in will open. Type the following:
That's our form designed. Now onto the coding
Double click on the display button, like in the last lesson and the code window will open. Type the following:
"Dim result as string" means that we are defining "result" as a variable of type string which we can then use within the sub
"Select Case cbohowout.text" is a function where the program works out what to do next based on the value of cbohowout.text (our combobox).
[normally we'd use selectedindex instead of text but I'm trying to keep it simple for the moment. See
here to see it done with selectedindex instead)
We then add our first case. In this example the code says that if the combo box is set to "bowled" it will give execute this line of code. In other words this is what "result" will equal.
result will equal
"txtbatsman.text" (the contents of the batsman text box)
"&" (the symbol used to join together more than one string)
" b " (space, letter b, space)
"&" (again)
"txtbowler.text" (the contents of the bowler text box)
"&"
" " (used to put a space between bowler and runs)
"txtruns.text" (the contents of the runs text box)
It's much simpler than it looks...
Next add in a Case statement for "caught"
Same principle as above, we've just added a fielder in for caught.
Code it for all the possibilites of the combobox and put a messagebox at the end (like in the first tutorial):
Finally go back to your form design, click on the view menu and select tab order. Change it to make it easier to navigate:
Run the program and post a screenshot here