Lauren
Lauren

Reputation: 77

How to reference text boxes that have been added in run time

My program creates a random number of TextBox controls when the form loads. I need to then reference what has been typed into these textboxes elsewhere in the code. My problem is is that an error comes up when I try to do so. Below is my code.

Dim userAct As New TextBox
userAct.Location = New Point(386, 379)
userAct.Size = New Size(95, 34)
userAct.Font = New Font("Luicida Fax", 12.0!)
userAct.BackColor = SystemColors.InactiveCaption
userAct.BorderStyle = BorderStyle.None
userAct.Multiline = True
Me.Controls.Add(userAct)

userAct is created but I cannot reference this textbox anywhere within the code. Is there anyway to overcome this? Thanks

Upvotes: 0

Views: 45

Answers (2)

David Wilson
David Wilson

Reputation: 4439

I'm guessing that you're declaring userAct in a sub/function procedure. Although the control is added to the form at runtime, the variable userAct goes out of scope after the sub/function is completed. There are several possible solutions to this, but would suggest the following

You can use the code you have, but also assign a "userAct" string to the .Name property and access the control using something like

directcast(Me.Controls.Find("userAct", False)(0),TextBox) 

This can then be used in exactly the same way as a normal TextBox ..

directcast(Me.Controls.Find("userAct", False)(0),TextBox).Text="Hi there"

or

Dim S as string = directcast(Me.Controls.Find("userAct", False)(0),TextBox)(0).Text

Upvotes: 1

djv
djv

Reputation: 15774

Use this to get a control by its name

<Extension()> _
Public Function ControlByName(Of T As Control)(ByVal parent As Control, ByVal name As String) As T
    For Each c As T In parent.ChildControls(Of T)()
        If c.Name = name Then
            Return c
        End If
    Next
    Return Nothing
End Function

Use this method to get all the controls on the form, even within containers

<Extension()> _
Public Function ChildControls(Of T As Control)(ByVal parent As Control) As List(Of T)
    Dim result As New List(Of T)
    For Each ctrl As Control In parent.Controls
        If TypeOf ctrl Is T Then result.Add(CType(ctrl, T))
        result.AddRange(ctrl.ChildControls(Of T)())
    Next
    Return result
End Function

(Control.Controls only returns controls contained immediately in Control)

In your case, this would be used like so

Dim userAct As New TextBox
userAct.Location = New Point(386, 379)
userAct.Size = New Size(95, 34)
userAct.Font = New Font("Luicida Fax", 12.0!)
userAct.BackColor = SystemColors.InactiveCaption
userAct.BorderStyle = BorderStyle.None
userAct.Multiline = True
userAct.Name = "userActName" ' new
Me.Controls.Add(userAct)

Dim myUserAct As TextBox = Me.ControlByName(Of TextBox)("userActName")

Alternatively, you can use this non-generic version

<Extension()> _
Public Function ControlByName(ByVal 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 List(Of Control)
    Return ChildControls(Of Control)(parent)
End Function

but you would need to cast

Dim myUserAct As TextBox = DirectCast(Me.ControlByName("userActName"), TextBox)

Upvotes: 2

Related Questions