类型检查 WebControl

Gol*_*hop 0 vb.net asp.net

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

Select Case TypeName(ctrl)
  case is = "ListBox"
    ...
  case is = "ComboBox"
    ...
  ...
End Select
Run Code Online (Sandbox Code Playgroud)

在 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
Run Code Online (Sandbox Code Playgroud)

igr*_*mpe 5

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

    Select Case True
        Case TypeOf c Is TextBox
            ' its a Textbox
        Case TypeOf c Is Label
            ' its a label
        Case Else
            'foo
    End Select
Run Code Online (Sandbox Code Playgroud)