VB2017 VB2015 VB2013 VB2012 VB2010 VB2008 VB6 VB Sample Codes 中文VB About Us

Lesson 14 Select Case


In this Lesson, you shall learn how to use the Select Case control structure in Visual Basic 2015. The Select Case control structure also involves decisions making but it slightly different from the If….ElseIf control structure . The If … Then...ElseIf statement control structure evaluates only one expression but each ElseIf statement computes different values for the expression. On the other hand, the Select Case control structure evaluates one expression for multiple values. Select Case is preferred when there exist multiple conditions.

14.1 The Select Case…End Select Structure

The structure of the Select Case control structure in Visual Basic 2015 is as follows:

Select Case expressionCase value1
Statements
Case value2
Statements
Case value3
.
.
Case Else
Statements
End Select

14.2 The usage of Select Case 

Example 14.1: Examination Grades

In this example, the program will display a message associated with the grade entered by the user.

The Code

Private Sub BtnShow_Click(sender As Object, e As EventArgs) Handles BtnShow.Click
Dim grade As String
grade = TxtGrade.Text
Select Case grade
Case “A”
MsgBox(”High Distinction”)
Case “A-”
MsgBox(”Distinction”)
Case “B”
MsgBox(”Credit”)
Case “C”
MsgBox(”Pass”)
Case Else
MsgBox(”Fail”)End Select
End Sub

The Output

VB2015_fig14.1
 Figure 14.1
VB2015_fig14.2

Figure 14.2

In this example, you can use the keyword Is together with the comparison operators to evaluate an expression.

Example 14.2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click’Examination Marks

Dim mark As Single
mark = mrk.Text
Select Case mark
Case Is >= 85
MsgBox( “Excellence”)
Case Is >= 70
MsgBox( “Good”)
Case Is >= 60
MsgBox( “Above Average”)
Case Is >= 50
MsgBox( “Average”)
Case Else
MsgBox( “Need to work harder”)
End Select

End Sub

Example 14.3

Example 14.2 can be rewritten as follows:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

‘Examination Marks

Dim mark As Single
mark = Textbox1.Text
Select Case mark

Case 0 to 49
MsgBox( “Need to work harder”)

Case 50 to 59
MsgBox( “Average” )

Case 60 to 69
MsgBox( “Above Average”)

Case 70 to 84
MsgBox( “Good”)

Case 85 to 100
MsgBox(“Excellence”)

Case Else
MsgBox( “Wrong entry, please reenter the mark”)

End Select

End Sub

Example 14.4

Grades in high school are usually presented with a single capital letter such as A, B, C, D or E. The grades can be computed as follow:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

‘Examination Marks

Dim mark As Single

mark = TextBox1.Text

Select Case mark

Case 0 To 49
LblGrade.Text = “E”

Case 50 To 59

LblGrade.Text = “D”
Case 60 To 69

LblGrade.Text = “C”
Case 70 To 79

LblGrade.Text = “B”

Case 80 To 100
LblGrade.Text = “A”

Case Else
LblGrade.Text = “Error, please re-enter the mark”

End Select

End Sub

The output

Figure 14.3


❮ Previous Lesson Next Lesson ❯


Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy