The aims of this example is to show you the different ways you can make choices.
Examples of code structures that allow you to make choices.
PreparationsCreate a new Windows Forms Project
Add a text box called ColorTextBox
Add a Picture box called SquareBox
Aim of next 4 examples
If user enter a preset color into the text box, change the color of the Square to that color. Otherwise it white
Example 1: IF {Condition} THEN {Matches Condition Code} ELSE {Doesn't Match Code} END IFAdd a button, change its text to "Example 1: IF" (Without the quotes of cause.)
Double click on the button
vb
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Example 1: IF
If Me.ColorTextBox.Text = "RED" Then
Me.SquarePB.BackColor = Color.Red
Else
Me.SquarePB.BackColor = Color.White
End If
If Me.ColorTextBox.Text = "GREEN" Then
Me.SquarePB.BackColor = Color.Green
Else
Me.SquarePB.BackColor = Color.White
End If
If Me.ColorTextBox.Text = "BLUE" Then
Me.SquarePB.BackColor = Color.Blue
Else
Me.SquarePB.BackColor = Color.White
End If
End Sub
Example 2: IF {Condition1} THEN {Matches Condition Code} ELSEIF {Condition2} ELSE {Doesn't Match Code} ENDIFAdd another button
Change text to "Example 2: IF .. ELSEIF ... ELSE ... ENDIF"
Add the code
vb
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' Example 2: IF ... THEN ... ELSEIF ... ELSE ... END IF
If Me.ColorTextBox.Text = "RED" Then
Me.SquarePB.BackColor = Color.Red
ElseIf Me.ColorTextBox.Text = "BLUE" Then
Me.SquarePB.BackColor = Color.Blue
ElseIf Me.ColorTextBox.Text = "GREEN Then" Then
Me.SquarePB.BackColor = Color.Green
Else
Me.SquarePB.BackColor = Color.White
End If
End Sub
Example 3: Variant of IF THEN Add another button
Change text to "Example 3: Variant IF .. THEN ... ENDIF"
Add this code
vb
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
' Example 3: Variant of IF ... THEN ...
Me.SquarePB.BackColor = Color.White
If Me.ColorTextBox.Text = "RED" Then Me.SquarePB.BackColor = Color.Red
If Me.ColorTextBox.Text = "BLUE" Then Me.SquarePB.BackColor = Color.Blue
If Me.ColorTextBox.Text = "GREEN Then" Then Me.SquarePB.BackColor = Color.Green
End Sub
Example 4
SELECT CASE {value}
CASE {Condition 1}: {Matches Condition Code}
CASE {Condition 2}: {Matches Condition Code}
CASE ELSE
{Doesn't Match Code}
END CASE
Add another button
Change text to "Example 4: SELECT CASE"
Add this code
vb
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
' Example 4: Select Case ... Case ... End Select
Select Case Me.ColorTextBox.Text
Case "RED" : Me.SquarePB.BackColor = Color.Red
Case "BLUE" : Me.SquarePB.BackColor = Color.Blue
Case "GREEN" : Me.SquarePB.BackColor = Color.Green
Case Else : Me.SquarePB.BackColor = Color.White
End Select
End Sub
IIF({Condition} , {Matches Code} , {Doesn't Match Code})Example: Negative or PositiveAdd to form another Text box call it NumberTextBox, with text "NEGATIVE OR POSITIVE"
Add another picture box to the form call it NegativePB
Add another button.
vb
'Example 5: IIF
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumberTextBox.TextChanged
' This makes sure only a number enables the button.
If System.Text.RegularExpressions.Regex.IsMatch(Me.NumberTextBox.Text, "^(-?\d+.\d|^-?\d)$") Then
Me.Button5.Enabled = True
Else
Me.Button5.Enabled = False
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
' Example 5: IIF - The inline if statement
Me.NegativePB.BackColor = IIf(Val(Me.NumberTextBox.Text) < 0, Color.Red, Color.Black)
End Sub
CHOOSE({value} ,{=1 Code}.{=2 Code}, etc)Value must greater or equal to one.
Example: MedalsAdd to the form another button
Add to the form another picture box called MedalPB
Add a radio button with text "First", checked is true
Add a radio button with text "Second", checked is false
Add a radio button with text "Third", checked is false
vb
' Example 6: Using CHOOSE
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
If Me.RadioButton1.Checked Then MedalState = 1
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
If Me.RadioButton2.Checked Then MedalState = 2
End Sub
Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
If Me.RadioButton3.Checked Then MedalState = 3
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Me.MedalPB.BackColor = Choose(MedalState, Color.Gold, Color.Silver, Color.Brown)
End Sub
Dim MedalState As Double = 1
I hope you have found this helpful.
Attached ProjectTutorial Project in a ZIP
ChoicesTutorual.zip ( 59.1k )
Number of downloads: 102