Many TextBoxes but uses the same events? Please help.

marksquall

Active member
Joined
Oct 17, 2009
Messages
28
Programming Experience
3-5
Dear administrators and members,

Hello everyone, a plesant day.:welcoming:

I got this code somewhere on the Internet by accident. The code automatically creates ten (10) textboxes on run time upon loading the form.

VB.NET:
[COLOR="#0000FF"]Private Sub[/COLOR] Main_Load([COLOR="#0000FF"]ByVal[/COLOR] sender [COLOR="#0000FF"]As[/COLOR] System.[COLOR="#0AEFD1"]Object[/COLOR], [COLOR="#0000FF"]ByVal[/COLOR] e [COLOR="#0000FF"]As[/COLOR] System.[COLOR="#0AEFD1"]EventArgs[/COLOR]) [COLOR="#0000FF"]Handles MyBase[/COLOR].Load

	[COLOR="#0000FF"]Dim[/COLOR] i[COLOR="#0000FF"] As Integer[/COLOR]

       [COLOR="#0000FF"] For[/COLOR] i = 1 [COLOR="#0000FF"]To[/COLOR] 10
            [COLOR="#0000FF"]Dim[/COLOR] ansTxtBox1 [COLOR="#0000FF"]As New[/COLOR] [COLOR="#0AEFD1"]TextBox[/COLOR]()
            [COLOR="#0000FF"]Dim[/COLOR] numLabel [COLOR="#0000FF"]As New[/COLOR] [COLOR="#0AEFD1"]Label[/COLOR]()
            numLabel.Text = i & [COLOR="#B22222"]"."[/COLOR]
            ansTxtBox1.Name = [COLOR="#B22222"]"TxtRead"[/COLOR] & i
            ansTxtBox1.Size = [COLOR="#0000FF"]New[/COLOR] Drawing.[COLOR="#0AEFD1"]Size[/COLOR](40, 23)
            
           [COLOR="#0000FF"] Me[/COLOR].FlowLayout.Controls.Add(numLabel)
            [COLOR="#0000FF"]Me[/COLOR].FlowLayout.Controls.Add(ansTxtBox1)

       [COLOR="#0000FF"] Next[/COLOR]
[COLOR="#0000FF"]End Sub[/COLOR]

Since I have created 10 TextBox objects with different names (and eventually 10 Label objects too), How can I create an only one event in all TextBox objects in which I will "collect" the text/string from the ten TexBox objects and maybe store each input in an array of String. I hope you could help me with this one.


Thank you and more power. :adoration:


Respectfully Yours,

MarkSquall
 
Last edited:
Look up AddHandler statement.
 
Collecting the string from "created-by-run-time" TextBox objects still not OK.

JohnH,

Thank you for your reply.

I am sorry but I am not so familiar yet with AddHandler, but what I did was I added a button and from there, I "collect" all the strings from my TextBox objects. Here is the code:

VB.NET:
[COLOR="#0000FF"]Private Sub[/COLOR] Button1_Click([COLOR="#0000FF"]ByVal[/COLOR] sender [COLOR="#0000FF"]As[/COLOR] System.[COLOR="#008080"]Object[/COLOR], [COLOR="#0000FF"]ByVal[/COLOR] e [COLOR="#0000FF"]As[/COLOR] System.[COLOR="#008080"]EventArgs[/COLOR]) [COLOR="#0000FF"]Handles[/COLOR] Button1.Click
       [COLOR="#0000FF"] Dim [/COLOR]x(10) [COLOR="#0000FF"]As Integer[/COLOR]
       [COLOR="#0000FF"] Dim[/COLOR] y [COLOR="#0000FF"]As Integer[/COLOR] = 0
        [COLOR="#0000FF"]Dim[/COLOR] i [COLOR="#0000FF"]As Integer[/COLOR] = 0
       
        [COLOR="#0000FF"]For Each[/COLOR] _control [COLOR="#0000FF"]As[/COLOR] [COLOR="#008080"]Control[/COLOR] [COLOR="#0000FF"]In Me[/COLOR].FlowLayout.Controls
            [COLOR="#0000FF"]Dim[/COLOR] senderbox [COLOR="#0000FF"]As[/COLOR] [COLOR="#008080"]Control[/COLOR] =[COLOR="#0000FF"] DirectCast[/COLOR](_control, [COLOR="#008080"]TextBox[/COLOR])
            [COLOR="#0000FF"]For[/COLOR] i = 1 [COLOR="#0000FF"]To[/COLOR] 10
                [COLOR="#0000FF"]If [/COLOR](_control.Name = ([COLOR="#800000"]"TxtRead"[/COLOR] & i.ToString())) [COLOR="#0000FF"]Then[/COLOR]
                    x(i) = _control.Text
                [COLOR="#0000FF"]End If
            Next
        Next
       
    End Sub[/COLOR]

But sadly, it has an error. It seems that my code gets the Label objects inside my FlowLayoutPanel (I named it as FlowLayout), and not the TextBox objects, that is why I received an InvalidCastException exception.

I think this is "almost" the right code.
But I my real target is when I enter "VB" in my first TextBox (TxtRead1) then the value of TxtRead1.Text (which is the string VB) will be saved to array x(1). In turn, if I enter "C#" in my second TextBox (TxtRead2) then the value of TxtRead2.Text (which is the string C#) will be saved to array x(2), so goes on with other TextBox object created dynamically at run time.

I hope someone could help me revise this Button1_Click code.

Thank you and more power.:welcoming:


Respectfully Yours,


Mark Squall
 
Last edited:
The question somehow seems different now, anyhow, you can check if an object is of a specific type using the TypeOf operator.
Your profile states .Net 2, for .Net 3.5 onwards this is much simpler done using the OfType extension for filtering.
 
Back
Top