Click here to Skip to main content
15,888,242 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I have a Dynamically created 100 textboxes on one form in 10X10 format. Similarly i have dynamically created 100 Labels on same form for each textbox.

Working of application is : Application is for lottery center. User will enter values in some textboxes. select rates. and will pay calculated amount.


I divided form controls of 10 rows of 10 controls for easy access for proper naming conventions. Here is the code that i am trying :

As these controls are dynamically created, i am accessing them using code in following snippet.

My Task is : Select the values in textbox on textchanged event from respective textbox, select text from label related to that textbox and put these values in array or database.


Anybody have any idea ?

VB
For Each TextBox As TextBox In pnlNumberContainer.Controls.OfType(Of TextBox)()
        For i As Integer = 0 To 10
            If TextBox.Name = "txtR1C" & i Then
                If Not TextBox.Text = "" Then
                    R1Quantity = R1Quantity + Integer.Parse(pnlNumberContainer.Controls(TextBox.Name).Text)
                    SelectedRate = RateChecker()
                    For Each Label As Label In pnlNumberContainer.Controls.OfType(Of Label)()
                        For j As Integer = 0 To 10
                            If Label.Name = "lblR1C" & j Then
                                If Not TextBox.Text = "" Then
                                    TicketNo = Integer.Parse(pnlNumberContainer.Controls(Label.Name).Text)
                                    ArrayString = TicketNo & "," & Integer.Parse(pnlNumberContainer.Controls(TextBox.Name).Text) & "," & SelectedRate

                                    TicketArray(counter) = ArrayString
                                    counter += 1

                                End If
                            End If
                        Next
                    Next
                End If
            End If
        Next
    Next
Posted
Comments
Kim Togo 1-Apr-15 8:30am    
Just hookup with TextChanged for all the TextBox that you create dynamically.
In TextChanged event, there is a "sender as Object". The "sender" will be your TextBox.
mrmalkar 1-Apr-15 8:38am    
Hmm, Thanks. I'll try that.
mrmalkar 1-Apr-15 8:42am    
Well, sender only gives 4 methods and no constructors. And methods are : Equals, GetHashCode, GetType, ToString.

@Kim : Can you please elaborate some more.
CHill60 1-Apr-15 8:44am    
Cast sender as TextBox to get the other properties. You'll have to wire-up the events yourself when creating the textboxes
mrmalkar 1-Apr-15 9:02am    
@CHill60 : Yes, i am creating the events myself.

And sure, i'll try by casting sender as TextBox

1 solution

Further to the comments here is an example of what we mean
VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim currTop As Integer = 10

    For i As Integer = 1 To 10
        Dim t As TextBox = New TextBox()
        t.Top = currTop
        t.Name = "tb" + i.ToString()

        Dim l As Label = New Label()
        l.Top = currTop
        l.Left = t.Left + t.Width + 10
        l.Name = "label" + i.ToString()
        l.Text = l.Name

        currTop += t.Height + 10

        'Wire up a common TextChanged handler
        AddHandler t.TextChanged, AddressOf MyText_TextChanged
        Me.Controls.Add(t)
        Me.Controls.Add(l)
    Next

End Sub

Private Sub MyText_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    'Get the control as a TextBox so we can access all properties
    Dim tb As TextBox = DirectCast(sender, TextBox)

    'Do whatever with the textbox...
    Dim ln As String = tb.Name
    ln = ln.Replace("tb", "label")

    Console.WriteLine("Text box {0} chosen. Label text is {1}", tb.Name, Me.Controls(ln).Text)
End Sub
 
Share this answer
 
Comments
mrmalkar 2-Apr-15 0:55am    
@CHill60 : Thank you for your reply. Generation of controls is already done. I'll try that DirectCast method and try to retrieve the data i wanted.
CHill60 2-Apr-15 10:06am    
I just left in the generation of the controls from my sample so that you could see where I used AddHandler.
Not sure what happened to the formatting of my code but hopefully it is still clear - give me a shout if you are still having problems

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