Question How do I add a newly created textbox to a dynamic Textbox array

batteryfirefox

New member
Joined
Aug 30, 2011
Messages
1
Programming Experience
5-10
I wanted to simply on click a button to add those newly created textboxes to an array of textboxes. Starting with those labeled "Address". But I am finding this very difficult. I am able to add those originally on the form into the array very easily. But am unable to add the newly created textboxes into the array.

Here is my code so far below.

Public Class Form1
Dim MyBoxes() As TextBox = {Address, UserName, Password}
Dim Numbox As Integer = 1

Sub CopyLayout(ByVal B As Integer)
'add new row
Dim cr As New UserControl1
cr.Left = 0
cr.Address.Name = "Address" & B
cr.Top = Me.Panel1.Controls.OfType(Of UserControl1).Count * cr.Height + 98
Me.Panel1.Controls.Add(cr)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Numbox = Numbox + 1
Call CopyLayout(Numbox)
ReDim Preserve MyBoxes(Numbox)
MyBoxes(Numbox) = Me.Panel1.Controls.Item("Address" & Numbox)
End Sub
End Class

Extra info:
UserControl1.vb simply contains three textboxes named approapriatly as "Address", "Username" and "Password".
 
With a List(Of TextBox):

Public Class FormListAddresses
    Private boxList As New List(Of TextBox)

    Sub AddRow()
        Dim count As Int32 = 1 + boxList.Count \ 3
        Dim current As New AddressControl
        current.Top = count * current.Height
        boxList.AddRange(current.GetBoxes)
        Me.PanelControls.Controls.Add(current)
    End Sub

    Private Sub ButtonAddRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles ButtonAddRow.Click
        AddRow()
    End Sub

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        boxList.AddRange(AddressControl1.GetBoxes)
    End Sub
End Class


Public Class AddressControl
    Public Function GetBoxes() As IEnumerable(Of TextBox)
        Return {TextBoxAddress, TextBoxUserName, TextBoxPassword}
    End Function
End Class




But I think it may be enough to hold a reference to the usercontrols instead:

Public Class FormListAddresses
    Private controlList As New List(Of AddressControl)

    Sub AddRow()
        Dim count As Int32 = 1 + controlList.Count
        Dim current As New AddressControl
        current.Top = count * current.Height
        controlList.Add(current)
        Me.PanelControls.Controls.Add(current)
    End Sub

    Private Sub ButtonAddRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles ButtonAddRow.Click
        AddRow()
    End Sub

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        controlList.Add(AddressControl1)
    End Sub
End Class
 
Me.Panel1.Controls.Item("Address" & Numbox)
This will return Nothing, because there are no controls by that name in the panels control collection. You have only added usercontrols without name to it. The controls within the usercontrol only exist within that Controls collection.
 
Back
Top