如果您的程式裡有很多的條件式語句, 用 If..Then..Else 來控制它的流程可能很麻煩。 在這種情形下, 最好是使用 Select Case.......End Select.
它的格式如下 :
Select Case 表達式 Case 數值1 VB 語句 Case 數值2 VB 語句 . . Case Else VB 語句 End Select
在这个例子里,您可以根据考试成绩而决定它的等级。
Dim grade As String Private Sub Compute_Click( ) grade=txtgrade.Text Select Case grade Case "A" result.Caption="特優" Case "A-" result.Caption="優等" Case "B" result.Caption="中等" Case "C" result.Caption="及格" Case Else
result.Caption="不及格" End Select End Sub<
Dim mark As Single Private Sub Compute_Click() mark = mrk.Text Select Case mark Case Is >= 85 comment.Caption = "特優" Case Is >= 70 comment.Caption = "優等" Case Is >= 60 comment.Caption = "中等" Case Is >= 50 comment.Caption = "及格" Case Else comment.Caption = "不及格" End Select End Sub<
範例 8.2 c 可以改寫成:
Dim mark As Single Private Sub Compute_Click()
'考試成績 mark = mrk.Text Select Case mark Case 0 to 49 comment.Caption = "不及格" Case 50 to 59 comment.Caption = "及格" Case 60 to 69 comment.Caption = "中等" Case 70 to 84 comment.Caption = "优等" Case Else comment.Caption = "特優" End Select End Sub