3.programming Conditional Logic
3.programming Conditional Logic
firstname = “FARAI"
Else
End If
Now run the programme and see what happens when you click the button.
You should have gotten a Message Box popping up saying "firstname is
not FADZAI". The reason is that we included the Else word. We're now
saying, "If the condition is met Then display one Message Box. If the
condition is not met, display a different Message Box." Notice that the Else
word is on a line of it's own.
Now, after you have tested your programme, try this. Add a textbox to your
form. Then change this line in your code:
firstname = “FARAI"
To this:
firstname = Textbox1.Text
What the code does is to transfer the text in the Textbox directly to
the firstname variable. We can then test what is in the variable with an If
statement.
When you've finished the code, test it out by typing the word “FADZAI"
(with a capital B) into the textbox, and then clicking the button. Then try it
with a lower case “f".
Select Case Statements
The Select Case statement is another way to test what is inside of
variable. You can use it when you know there is only a limited num
of things that could be in the variable. For example, suppose we a
another choice for that cream cake. We've only said that the
consequences of eating the cream cake is that the Diet will be eith
"Ruined" or "Not Ruined". But what if we add another option - "Diet
Tested". In other words, we ate the cake but refused to climb onto
scales to weigh ourselves!
With three choices, we can still use an If ... Else statement. But le
change it to a Select Case statement. Remember: all we are doing
testing what is inside a variable, in this case a variable
called creamcake. Once we decide what is inside the variable, we
take some action. So let's look at how the Select Case works.
Create a Form with a button and a Textbox on it (If
you have your form open from the previous section,
then you can use this one). Double click the new
button. You should see something like this appear.
Private Sub Button1_Click(ByVal sender As
System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
End Sub
Between the button Sub and End Sub code add the
folowing
Dim creamcake As String
Dim DietState As String
creamcake = TextBox1.Text
Case "Eaten"
DietState = "Diet Ruined"
Case "Not Eaten"
DietState = "Diet Not Ruined"
Case Else
DietState = "Didn't check"
End Select
MsgBox DietState
Run your code to test it out. Click inside your textbox and enter the word "Eaten". Then click your button to
see what happens. Now enter the words "Not Eaten" and click your button. Next, try the word "eaten", with a
lowercase "e".
You can use Select Case statement with numbers, as well, and the To word comes in
very handy here. If you were checking a variable to see if the number that was in the
variable fell within a certain age-range, you could use something like this:
Select Case agerange
Case 16 To 21
MsgBox “Still Young”
Case 50 To 64
MsgBox “Start Lying”
End Select
Here, a variable called agerange is being tested. It's being checked to see if it falls
between certain values. If it does, then a message box is displayed.
A popular way to use Select Case statements is with a drop down box. You can
then test which item a user selected from a list of available items. You're going to
write a little programme to do just this. But before you can do so, you'll need to
know how to add a Combo Box to a form, and how to get at the values in its list.
We'll do that in the next section
Add a Combo Box to a VB .NET form
Create a new project for this section. Add a button to your new form. Then, locate the Combo Box on the Visual
Basic .NET toolbar. It looks like this:
Double click the icon to add a Combo Box to your form. Or click once with the left hand mouse button, and then draw
one on the form.
A combo box is a way to limit the choices your user will have. When a black down-pointing arrow is clicked, a drop
down list of items appears. The user can then select one of these options. So let's set it up to do that.
•Click on your Combo Box to select it. Then locate the Item property from the Properties Box:
Click the grey button, as above. The one with the three dots in it. When you do, you'll get the following box
popping up:
•To use the String Collection Editor, type an item and press Return (it's just like a normal textbox. Each item will be one
item in your drop-down box.)
•Enter five items, as in the image below:
•Then click the OK button at the bottom.
The Editor will close, and it will look like nothing has happened. However, run your programme and test out your new
Combo Box. You should have something like this:
You now need to know how to get values from the list. Once you know how to get a value from the list, you can put
the value into a variable and test it with some Conditional logic.
Getting a value from a Combo Box is fairly straightforward,
because it acts just like a Textbox. A Textbox has a Text
property, and so does a Combo Box. To get a value from a
Textbox, you would code like this
MyVariable = Textbox1.Text
Whatever is in the Textbox will be transferred to the
variable called MyVariable. The process is exactly the
same for a Combo Box. The code is like this:
MyVariable = Combobox1.Text
Now we are transferring whatever is selected from the Combo Box to the
variable called MyVariable.
Let's try it. Double click the button you added to your form. This will open
the code window. Then enter the following code for the button:
Dim MyVariable as String
MyVariable = Combobox1.Text
MsgBox MyVariable
Run your programme. When the programme is running, select an item from
your Combo Box. Then click your button. Whatever was in the Combo Box
window should have ended up in the Message Box.
And that's all there is to getting a value from a
Combo Box - just access its Text Property and pass
it to a variable.
Finally, the Combo Box has
a DropDownStyle property. Locate this property and
you'll notice its value has a drop down box. The box
contains three different Combo Box styles to
choose from. Experiment with all three and see how
they differ.
In the next section, we'll take a look at Conditional
Operators, what they are, and how to use them.
Conditional Operators
The Conditional Operators allow you to refine
what you are testing for. Instead of saying "If X
is equal to Y", you can specify whether it's
greater than, less than, and a whole lot more.
Examine the list of Operators:
word about And and Or. Notice the format with And and Or. The variable is
repeated twice
If VariableName= 7 Or VariableName= 10 Then MsgBox "7
or 10 spotted"
If you just put something like this
If VariableName> 7 Or < 10 Then MsgBox "7 or 10 spotted"
then Visual Basic will give you an error message. What you have to say is
If [test the variable for this value] And [test the variable for
that value] Then
Your If statement can contain an Else part, too. The format is this:
If [conditional logic here] Then
Some Code here
Else
Some Other Code here
End If
Section Three Exercises
Part 1 - If statements
Start a new project. Add a textbox, a Label and a button to your new Form. Then write
a programme that does the following:
1.Asks users to enter a number between 10 and 20.
2.The number will be entered into the Textbox.
3.When the Button is clicked, your Visual Basic code will check the number entered in
the Textbox.
4.If it is between 10 and 20, then a message will be displayed.
5.The message box will display the number from the Textbox.
6.If the number entered is not between 10 and 20 then the user will be invited to try
again, and whatever was entered in the Textbox will be erase
Part 2 - Select Case Statements
Add a Combo box and another button to your form. Create a list of items for your Combo
Box. The list of items in your Combo box can be anything you like - pop groups, football
teams, favourite foods, anything of your choice. Then try the following:
Use a select case statement to test what a user has chosen from your drop-down
list. Give the user a suitable message when the button was clicked.