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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15339139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:44:01  来源:igfitidea点击:

Using string as object name

vb.net

提问by conquistador

I'm trying to use string as object name. Example I have an object and has a name = Label1. Can I do this?

我正在尝试使用字符串作为对象名称。示例我有一个对象并且有一个名称 = Label1。我可以这样做吗?

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

I'm using string as object name, is it possible?

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

回答by Chris Haas

You could iterate over all of the controls as @Christian Sauer said but you might run into problems if any controls are containers of controls. You'd need to do a recursive search to solve that. However, the ControlCollectionactually has a Find()method that you can use. It returns an array of controls that match the name and optionally performs a recursive search.

您可以像@Christian Sauer 所说的那样迭代所有控件,但如果任何控件是控件的容器,您可能会遇到问题。你需要做一个递归搜索来解决这个问题。但是,ControlCollection实际上有一种Find()方法可以使用。它返回与名称匹配的控件数组,并可选择执行递归搜索。

    ''//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

回答by Christian Sauer

This is not possible - but what you can do:

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

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

I am iterating over all WinForms control to find the label with the Name "Label1" - when found, i assign the label to a Variable. This works, but can be quite dangerous, especially if you add controls

我正在遍历所有 WinForms 控件以找到名称为“Label1”的标签 - 找到后,我将标签分配给一个变量。这有效,但可能非常危险,特别是如果您添加控件

回答by djv

I know it's been answered, but this is from my library, and I use it all the time. It will iterate over all controls, and containers' controls recursively as @ChrisHaas suggested.

我知道已经有人回答了,但这是来自我的图书馆,我一直在使用它。它将按照@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

(It's been asked and answered before) Loop Through Controls on Web User Control

(之前有人问过并回答过) Web User Control 上的 Loop Through Controls

回答by Ruba

I'm sure it's answered but some points to be clear it's control array and result so as to be sure it's corrected.

我确定它已得到回答,但有几点需要明确它是控制数组和结果,以确保它已得到纠正。

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