Using a TextBox Defined in code

revanthb3000

New member
Joined
Jan 7, 2012
Messages
4
Programming Experience
Beginner
Hi everyone, I'm new to this forum and VB

Recently I started making an application that would perform matrix multiplication. For that I need to take in the matrix data(row,column, elements). I've asked the user about number of rows and columns in a form. I then send him to another form which will contain row*column number of textboxes used to enter the elements.

After reading a bit about the TextBox Class I used the following code to create the variable number of textboxes.

VB.NET:
        For rcounter = 1 To row
            For ccounter = 1 To column

                Dim strName = "TextBox" & rcounter & ccounter
                Dim txt As New TextBox()
                Dim sz As New Size(20, 25)
                Dim pt = New Point(xpos, ypos)

                txt.Name = strName
                txt.Size = sz
                txt.Location = pt
                txt.Visible = True
                txt.TextAlign = HorizontalAlignment.Left
                Me.Controls.Add(txt)
                xpos = xpos + 25

            Next
            ypos = ypos + 30
            xpos = (200 - ((row * 25) / 2))
        Next

This code works well without problems.

My question is : How do I get the data that the user enters into these boxes ? Using the usual textbox.text gives me a build error.

Thanks in Advance,
RB
 
Your forum profile indicates .Net 1.0, so either that is wrong or you should definitely upgrade to current version. Answer is for .Net 2.0 or later.

You can simplify that a lot by using the TableLayoutPanel control. Add a TableLayoutPanel to form first, by adjusting control size in designer now you can set the initial row and column sizes. Then here's an example creating the control table:
        Me.TableLayoutPanel1.AutoSize = True
        Me.TableLayoutPanel1.RowCount = 3
        Me.TableLayoutPanel1.ColumnCount = 3
        For row = 0 To Me.TableLayoutPanel1.RowCount - 1
            For column = 0 To Me.TableLayoutPanel1.ColumnCount - 1
                Dim tb As New TextBox
                tb.Size = New Size(20, 25)
                Me.TableLayoutPanel1.Controls.Add(tb, column, row)
            Next
        Next

Similar loop can be used to get values:
        For row = 0 To Me.TableLayoutPanel1.RowCount - 1
            For column = 0 To Me.TableLayoutPanel1.ColumnCount - 1
                Dim c As Control = Me.TableLayoutPanel1.GetControlFromPosition(column, row)
                Debug.WriteLine("column {0} row {1} value {2}", column, row, c.Text)
            Next
        Next
 
About .NET Framework. That's a mistake while filling the form. Thanks for pointing that out.

Table Layout Panel. Looks like an interesting feature. Things are so easier now. Thank you very much.


But what's the answer to my question ? I mean, how do you use a textbox that you define in the code ?
 
But what's the answer to my question ? I mean, how do you use a textbox that you define in the code ?
You can access it from the Controls collection it was added to, either by integer index or string index (the Name of the control).
You can also manage your control references with a variable, of collection or array type, if that would make things any easier for you.
 
okay...

one more thing, how do i prevent the user from entering anything other than numbers into these textboxes ? How do i define the keypress event of these boxes ?
 
Got it. AddHandler seems to be pretty useful. Associating the same handler to multiple events is exactly what I need.

Thank you very much John :)
 
Back
Top