在VB.NET中,可以用以下代码来通过字符串名称获取控件:

Dim ctl As Control = Me.Controls(strControlName)

其中,strControlName是要获取的控件的名称,Me是当前窗体的实例。这个代码会返回一个Control类型的对象,可以根据需要进行类型转换。如果控件不在当前窗体中,可以使用以下代码:

Dim ctl As Control = FindControlRecursive(Me, strControlName)

其中,FindControlRecursive是递归查找控件的函数,代码如下:

Private Function FindControlRecursive(ByVal parent As Control, ByVal ctlName As String) As Control Dim ctl As Control = parent.FindControl(ctlName) If ctl IsNot Nothing Then Return ctl End If For Each child As Control In parent.Controls ctl = FindControlRecursive(child, ctlName) If ctl IsNot Nothing Then Return ctl End If Next Return Nothing End Function

这个函数会在当前控件及其子控件中递归查找指定名称的控件,如果找到了就返回该控件,否则返回Nothing

用vbnet通过字符串名称获取控件的函数

原文地址: https://www.cveoy.top/t/topic/hqb9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录