简体   繁体   中英

Using string as object name

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

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

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

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

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 ControlCollection actually has a Find() method that you can use. It returns an array of controls that match the name and optionally performs a recursive search.

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM