Vb.net 使用字符串作为对象名

Vb.net 使用字符串作为对象名,vb.net,Vb.net,我正在尝试使用字符串作为对象名。示例I有一个对象,名称为Label1。我能做这个吗 Dim i As String = "Label1" someVariable = i.Text 我使用字符串作为对象名,可能吗?这是不可能的-但您可以做什么: Dim i As String = "Label1" Dim Obj as Label for each elem in me.controls if elem.Name = i then Obj = elem exit

我正在尝试使用字符串作为对象名。示例I有一个对象,名称为Label1。我能做这个吗

Dim i As String = "Label1"
someVariable = i.Text

我使用字符串作为对象名,可能吗?

这是不可能的-但您可以做什么:

Dim i As String = "Label1"

Dim Obj  as Label
for each elem in me.controls
   if elem.Name = i then
     Obj =  elem
     exit for
   end if
next

someVariable = obj.Text
我正在迭代所有WinForms控件以查找名为“Label1”的标签-找到标签后,我将标签分配给一个变量。
这是可行的,但可能非常危险,特别是如果您添加控件,您可以像@Christian Sauer所说的那样迭代所有控件,但是如果任何控件都是控件的容器,您可能会遇到问题。你需要做一个递归搜索来解决这个问题。但是,
ControlCollection
实际上有一种方法可以使用。它返回与名称匹配的控件数组,并可选地执行递归搜索

    ''//Our final control
    Dim someVariable As Control = Nothing
    ''//Search recursively for our control
    Dim SearchedControls = Me.Controls.Find(key:="Label1", searchAllChildren:=True)
    ''//If we found one and only one control
    If SearchedControls.Count = 1 Then
        ''//Set it to our variable
        someVariable = SearchedControls(0)
    Else
        ''//Whatever your logic dictates here
    End If

我知道它已经被回答了,但这是我的图书馆里的,我一直在用它。它将迭代所有控件和容器控件,如@ChrisHaas所建议的那样

Public Function GetControlByName(ByRef parent As Control, ByVal name As String) As Control
    For Each c As Control In parent.ChildControls
        If c.Name = name Then
            Return c
        End If
    Next
    Return Nothing
End Function

<Extension()> _
Public Function ChildControls(ByVal parent As Control) As ArrayList
    Return ChildControls(Of Control)(parent)
End Function

<Extension()> _
Public Function ChildControls(Of T)(ByVal parent As Control) As ArrayList
    Dim result As New ArrayList()
    For Each ctrl As Control In parent.Controls
        If TypeOf ctrl Is T Then result.Add(ctrl)
        result.AddRange(ChildControls(Of T)(ctrl))
    Next
    Return result
End Function
公共函数GetControlByName(ByRef父函数作为控件,ByVal名称作为字符串)作为控件
对于parent.ChildControls中的每个c As控件
如果c.Name=Name,则
返回c
如果结束
下一个
一无所获
端函数
_
作为ArrayList的公共函数ChildControls(ByVal父控件作为控件)
返回子控件(共有控件)(父控件)
端函数
_
公共函数ChildControls(Of T)(ByVal parent作为控件)作为ArrayList
将结果变暗为新的ArrayList()
对于父控件中的每个ctrl As控件
如果ctrl的类型为T,则结果添加(ctrl)
结果.AddRange(儿童对照组(共T组)(ctrl))
下一个
返回结果
端函数
(以前有人问过,也有人回答过)

我肯定答案是肯定的,但有几点需要澄清,那就是它的控制阵列和结果,以确保它得到纠正

Private Sub btn1_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
    Dim Ds As New DataSet

    Dim str As String = ""
    str = " SELECT     TOP (10) t_Suppliers.STNo  from t_Suppliers  "
     Ds = SqlHelper.ExecuteDataset(ConnectionString, CommandType.Text, str)
    For i As Integer = 0 To Ds.Tables(0).Rows.Count - 1
        Dim str1 As String = "lblInv" & i + 1
        Dim OBj As New Label
        Try

       Dim SearchedControls() As Control = Me.Controls.Find(key:=str1, searchAllChildren:=True)
           If SearchedControls.Length > 0 Then
                SearchedControls(0).Text = Ds.Tables(0).Rows(i).Item("STNo").ToString
            End If
        Catch
        End Try
    Next
End Sub

我在另一个网站上找到了以下解决方案。 它起作用了

--引述-


在这种情况下我不知道Find。非常有帮助,谢谢
    Dim TextBox As TextBox
    Dim I As Integer = 2
    Dim name As String = "TextBox" & I.ToString
    TextBox = Me.Controls.Item(name)
    TextBox.Text = "Something special"