Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become an Expert!

Join 244,296 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 856 people online right now. Registration is fast and FREE... Join Now!




Choices

 
Reply to this topicStart new topic

> Choices, IFs, SELECT CASE. CHOOSE, IIF

AdamSpeight2008
Group Icon



post 29 Jul, 2008 - 05:57 PM
Post #1


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.
Preparations
Create 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 IF
Add 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} ENDIF
Add 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 Positive

Add 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: Medals
Add 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 Project
Tutorial Project in a ZIP
Attached File  ChoicesTutorual.zip ( 59.1k ) Number of downloads: 102
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

rakyomin
**



post 28 Oct, 2008 - 06:19 AM
Post #2
Great tutorial for explaining the basics. Good work.
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 7/4/09 04:41PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month