【发布时间】:2013-06-04 20:27:20
【问题描述】:
我正在尝试创建一个通用过程,只需使用您可以看到的参数调用此过程,即可将“X”控件对象的可见属性更改为“False/True”:
' Desired usage:
' Disable_Controls(CheckBox, Me.Panel1.Controls, False)
Public Sub Disable_Controls(ByVal ControlType As Control, _
ByVal Container As ControlCollection, _
ByVal Visible As Boolean)
For Each Control As Control In Container
' If TypeOf Control Is CheckBox then...
If TypeOf Control Is Control Then
Control.Visible = Visible
End If
Next
End Sub
问题是我不能像我正在尝试那样传递控件名称(“复选框”), 我用“CType(Control, CheckBox)”尝试了一些东西,但没有奏效。
我该怎么做?
【问题讨论】:
-
为什么
ControlType的类型是Control而不是Type?typeof control将始终返回 true,因为您将As'ing 为上面的Control。我不确定目标是什么。是搜索ControlCollection,找到某种类型的所有控件(例如Checkbox)并禁用它们? -
@Eli Gassert 是的,这就是目标,但我想通过使用“ControlName”调用 sub 来使其通用以处理所有类型的控件(复选框、按钮等)参数作为第一个参数。
-
@Eli Gasser 问你的第一个问题,ControlType 是“Control”而不是“Type”,因为我不知道这样做的好方法,现在我把它改成了“Type “而且我还在尝试。谢谢你的建议。
标签: .net vb.net visual-studio-2012 casting typeof