我在VBA中知道如何对控件Type进行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
答案 0 :(得分: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
答案 1 :(得分:2)
我认为你期待这样的事情 您可以使用TypeOf Operator
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