Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 将控件类型传递给过程_.net_Vb.net_Visual Studio 2012_Casting_Typeof - Fatal编程技术网

.net 将控件类型传递给过程

.net 将控件类型传递给过程,.net,vb.net,visual-studio-2012,casting,typeof,.net,Vb.net,Visual Studio 2012,Casting,Typeof,我正在尝试创建一个通用过程,通过使用您可以看到的参数调用此过程,将“X”控件对象的visible属性更改为“False/True”: ' Desired usage: ' Disable_Controls(CheckBox, Me.Panel1.Controls, False) Public Sub Disable_Controls(ByVal ControlType As Control, _ ByVal

我正在尝试创建一个通用过程,通过使用您可以看到的参数调用此过程,将“X”控件对象的visible属性更改为“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)”的方法,但没有成功

我怎么能做到

Public Sub Disable_Controls(Of T As Control)(ByVal Container As Control, _
                            ByVal Visible As Boolean)
    For Each ctrl As T In Container.Controls.OfType(Of T)()
        ctrl.Visible = Visible
    Next
End Sub
可以这样称呼:

Disable_Controls(Of Checkbox)(MyGroupbox, False)

为什么
ControlType
Control
类型,而不是
类型<代码>控件类型
将始终返回true,因为您正在将其作为上面的
控件
使用。我不确定目标是什么。是要搜索
ControlCollection
并找到特定类型的所有控件(例如
Checkbox
)并禁用它们吗?@Eli Gassert是的,这是目标,但我想通过使用“ControlName”调用sub,使所有类型的控件(复选框、按钮等)都通用化参数作为第一个参数。@Eli Gasser问你的第一个问题,ControlType是“Control”而不是“Type”'因为我不知道这样做的好方法,现在我把它改为“Type”,我还在尝试。谢谢你的建议。是
Container.OfType
将其所有内容过滤为该类型的内容,还是尝试将该容器的所有元素强制转换为该类型?如果是前者(过滤器),那么答案很好,很优雅!谢谢,但是sub在“For”行(T)抛出了一个异常:T是一个类型,不能用作表达式,这与我一直试图传递/转换第一个参数时遇到的错误相同。@Joel Coehoorn正在等待更新以解决您答案中的(T)异常,谢谢
.OfType()
过滤和强制转换所有内容。如果传递父类型,它将保留子控件。例外情况是,我缺少一组括号和关键字的
。它现在应该可以工作了。@Joel Coehoorn不工作:错误1类型参数不能用作限定符。在“T.可见=可见”行