0

我知道在 VBA 中如何对控件类型进行 Select..Case 比较,例如:

Select Case TypeName(ctrl)
  case is = "ListBox"
    ...
  case is = "ComboBox"
    ...
  ...
End Select

在 VB.Net 中,我可以使用上面的通用值,还是必须在文本中使用命名空间限定符?

目前实施:

public function Validate(byref ctrl as WebControl) as boolean
  select case TypeName(ctrl)
    case is = "TextBox"
      ....
    case is = "Label"
      ....
    ...
  End select
End Function
4

2 回答 2

4

您不需要类型的“名称”,您可以直接使用类型:

    Select Case True
        Case TypeOf c Is TextBox
            ' its a Textbox
        Case TypeOf c Is Label
            ' its a label
        Case Else
            'foo
    End Select
于 2012-12-11T16:24:44.863 回答
2

我想你期待这样的事情你可以使用TypeOf 运算符

Dim ctrl As Control
        For Each ctrl Me.Controls
            If (TypeOf ctrl Is TextBox) Then
                ''do something
            End If
            If (TypeOf ctrl Is Label) Then
                ''do something
            End If
        Next ctrl 

更新

使用case

  select case True
    case  TypeOf ctrl Is TextBox
      ....
    case TypeOf ctrl Is Label
      ....
    ...
  End select
于 2012-12-11T16:24:26.813 回答