英文VB教程 简体Visual Basic教程 繁体Visual Basic教程

第八课:Select Case 的应�



8.1 Select Case的应�

如果您的程序中有很多的条件式语句, � If..Then..Else 来控制它的流程可能会很麻烦� 在这种情形下, 最好是使用

Select Case.......End Select

它的格式如下 :

Select Case  表达�

   Case 数�
         VB 语句
  
Case 数�
        VB 语句
  
Case 数�
        VB 语句
        .
        .
  
Case Else
        VB 语句

End Select

8.2 范例

范例 8.1

在这个例子中,您可以根据考试成绩而决定它的等级�

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

范例 8.2

Dim mark As Single
Private Sub Compute_Click()
'Examination Marks

 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




版权所�©2008 Dr.Liew Voon Kiong。保留所有权� 。联系我�: VB面子�

[Privacy Policy]