Click here to Skip to main content
15,887,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created following class to create Textbox Array Dynamically.
Below is TextBoxArray.VB file
VB
Public Class TextBoxArray

    Inherits System.Collections.CollectionBase
    Private ReadOnly HostForm As System.Windows.Forms.Control
    Dim aTextBox As New System.Windows.Forms.TextBox
    Dim nIndex As Integer
    Dim nText As String
    Dim nLocation As Point
    Dim nParent As System.Windows.Forms.Control
    Dim nVisible, nEnable As Boolean
    Dim nSize As Size
    Dim nBorderStyle As BorderStyle
    Dim nTextAlign As ContentAlignment
    Dim nFont As Font
    Dim nName as string 

    Public Sub New(ByVal host As System.Windows.Forms.Control)
        HostForm = host
        Me.AddTextBox()
    End Sub

    Public Function AddTextBox() As System.Windows.Forms.TextBox
        ' Create a new instance of the Button class.

        ' Add the button to the collection's internal list.
        aTextBox = New TextBox
        Me.List.Add(aTextBox)
        ' Add the button to the controls collection of the form 
        ' referenced by the HostForm field.
        HostForm.Controls.Add(aTextBox)
        ' Set intial properties for the button object.
        With aTextBox
            .Location = nLocation
            .Tag = Me.Count
            .Text = nText
            '.BackColor = Color.Transparent
            .BorderStyle = Windows.Forms.BorderStyle.FixedSingle
            'aTextBox.Parent = nParent
            .Name = "TextBox" & Me.Count.ToString
            nName = .Name
            .Visible = False
            'AddHandler aTextBox.Click, AddressOf TextBox_Click
        End With
        Return aTextBox
    End Function

    Public Sub RemoveTextBox()
        ' Check to be sure there is a button to remove.
        While Me.Count > 0
            HostForm.Controls.Remove(Me(Me.Count - 1))
            Me.List.RemoveAt(Me.Count - 1)
        End While
    End Sub

    Default Public ReadOnly Property Item(ByVal Index As Integer) As  _
    System.Windows.Forms.TextBox
        Get
            Return CType(Me.List.Item(Index), System.Windows.Forms.TextBox)
        End Get
    End Property

    Public Property Index() As Integer
        Get
            Return nIndex
        End Get
        Set(ByVal value As Integer)
            nIndex = value
        End Set
    End Property

    Public Property Size() As Size
        Get
            Return nSize
        End Get
        Set(ByVal value As Size)
            nSize = value
            aTextBox.Size = value
        End Set
    End Property

    Public Property Location() As Point
        Get
            Return nLocation
        End Get
        Set(ByVal value As Point)
            nLocation = value
            aTextBox.Location = nLocation
        End Set
    End Property

    Public Property Text() As String
        Get
            Return nText
        End Get
        Set(ByVal value As String)
            nText = value
            aTextBox.Text = value
        End Set
    End Property

    Public Property Visible() As Boolean
        Get
            Return nVisible
        End Get
        Set(ByVal value As Boolean)
            nVisible = value
            aTextBox.Visible = value
        End Set
    End Property

    Public Property BorderStyle() As BorderStyle
        Get
            Return nBorderStyle
        End Get
        Set(ByVal value As BorderStyle)
            nBorderStyle = value
            aTextBox.BorderStyle = value
        End Set
    End Property

    Public Property AlignText() As ContentAlignment
        Get
            Return nTextAlign
        End Get
        Set(ByVal value As ContentAlignment)
            nTextAlign = value
            aTextBox.TextAlign = value
        End Set
    End Property

    Public Property Parent() As System.Windows.Forms.Control
        Get
            Return nParent
        End Get
        Set(ByVal value As System.Windows.Forms.Control)
            nParent = value
            aTextBox.Parent = value
        End Set
    End Property

    Public Property Enable()
        Get
            Return nEnable
        End Get
        Set(ByVal value)
            nEnable = value
            aTextBox.Enabled = value
        End Set
    End Property

    Public Property Font() As Font
        Get
            Return nFont
        End Get
        Set(ByVal value As Font)
            nFont = value
            aTextBox.Font = nFont
        End Set
    End Property

    Public Property Name() As String
        Get
            Return nName
        End Get
        Set(ByVal value As String)
            nName = value
            aTextBox.Name = nName
        End Set
    End Property

End Class


And to Display 3 Textbox Dynamically I do following steps in Main Form,

VB
Public Class Main

    Dim txt(3) As TextBoxArray

    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Left, Top As Integer
        Left = 22
        Top = 30
        For I = 0 To 2
            txt(I) = New TextBoxArray(Me)
            txt(I).Location = New Point(Left, Top)
            txt(I).Visible = True
            Top += 30
        Next
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For I As Integer = 0 To 2
            MsgBox("TextBox:" & I & txt(I).Text)
        Next
    End Sub
End Class


Above code displays 3 text box successfully but my problem is i don't know how to get the text entered inside the text box.
I used Button1 to get the value of tetxbox and
VB
txt(Index).Text

will not gives me anything available in the text box.

Can anybody help me on this.
I just wants to retrieve the text entered in the text box.
Posted

1 solution

Sorry to break this to you, but this is pretty much a coding horror.
To create an Array of TextBoxes the following would have sufficed:
VB
' Creates an Array that has room for 3(!) TextBoxes.
Dim textBoxArray(2) As TextBox
' They still have to be set, like following:
textBoxArray(0) = New TextBox
' Alternatively you can use a List(Of TextBox).

All the Properties you have defined in your redundant (and horribly written) TextBoxArray Class are already Properties of the Textbox.
You can set them like this:
VB
' Assuming there is a TextBox at the first index of your array
' textBoxArray(0) returns a TextBox.
textBoxArray(0).Visible = False
textBoxArray.Location = New Point(20, 30)
' Etc.
' Possibly do this in a For Loop.

Furthermore in your For Loop you create a new Array every time, making you lose your old Array (and references to it).
To get the Text Property of any one of the TextBoxes, simply ask for it.
VB
' Use MessageBox rather than MsgBox. MsgBox is a pre-.NET version of the MessageBox.
MessageBox.Show("TextBox1's text is: " & textBoxArray(0).Text)

This has nothing to do with Events (as your question title states).
To dynamically handle Events use the AddHandler and RemoveHandler keywords.
VB
' To add an Event Handler:
AddHandler textBoxArray(0).TextChanged, AddressOf TextBox_TextChanged
' To remove an Event Handler:
RemoveHandler textBoxArray(0).TextChanged, AddressOf TextBox_TextChanged

' The Method that handles it:
Private Sub TextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
   ' Code to handle the Event here.
End Sub

Your code above shows that you have not understood some basic programming rules.
I suggest you start reading up on things. Buy a book, read some tutorials and articles on the internet (CodeProject is a good place to start!).
Hope it helps and good luck!
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900