How to create array textboxes in Tab page.

From Input Box user will change TextBox Count and Tab Page count

Recommended Answers

All 10 Replies

When you add the textboxes to the tabpage, the tabpage automatically creates an array for them, the same for the tabpages. Since you would have to use a loop to add them,a separate array in this case would probably be redundant. Here's some code that adds the textboxes and the tabpages the amounts adjusted according to variables, that might help:

Public Sub AddPages(TPCount As Integer, TBCount As Integer)
    TabControl1.TabPages.Clear()
    For I = 1 To TPCount
        Dim NewPage As New TabPage("Tab " + I.ToString)
        For J = 1 To TBCount
            Dim TB As New TextBox
            SetupTB(TB)
            TB.Name = "Textbox " + I.ToString + "-" + J.ToString
            'This puts the textboxes in a column you can adjust this however you need.                
            TB.Location = New Point(TabControl1.Location.X, TabControl1.Location.Y + (J * (TB.Size.Height + 5)))
            NewPage.Controls.Add(TB)
        Next
        NewPage.BackColor = Color.Gray
        NewPage.Name = NewPage.Text
        TabControl1.TabPages.Add(NewPage)
    Next
    TabControl1.Refresh()
End Sub
Private Sub SetupTB(ByRef TB As TextBox)
    'set common properties here
End Sub

Thanks for reply.

When you code line 7 and 19-21 I can't understand really.
Here the code what I used to created Tab Pages from Input variable.

    'RecCount = User input Number
    TabControl1.TabPages.Clear()
    For xXx = RecCount To 1 Step -1 ' to Desending Order ---->3,2,1
    newPage = New TabPage 'create new instance
    If xXx = 1 Then
    newPage.Text = "Repeat - 1"
    Else
    newPage.Text = "Repeat - " & xXx.ToString
    End If
    TabControl1.TabPages.Add(newPage)
    Next

with this code Tab pages created according RecCount
Now I want to crate textboxes in Tab Pages and Write MS-Access Column Text to TextBoxes which we created on tab pages.

When you code line 7 and 19-21 I can't understand really.

It's just like what the comment says. It's a way to set up the common properties for every textbox on the page. things like font, forecolor, backcolor, etc. If the default properties are fine you can delete that part.

change lines 3&4 like this:

 For I = TPCount To 1 Step -1
    Dim NewPage As New TabPage("Repeat - " + I.ToString)

Then call the sub passing it RecCount and the variable you use for the number of textboxes you want.

With that code the textboxes names will include the page number and an index for the textbox, so that on every page each textbox in the same location will have the same index.. It will be a fairly simple matter to find the textboxes and add data to them.

Yes, I understand it,
Why You put "J" loop, "J" value always pass "1", why that ?

Other thing How can I call Table data to TextBoxes what we creating now. How I code to textboxes from Database.
According to this textbox name like = Textbox2-1
How I pass a value, to this kind of textbox name from code.

TB.Name = "Textbox " + I.ToString + "-" + J.ToString

You can use the substring method, it's intrinsic to every string. The value too look at will be at the same index in the name. for instance TB.Name.Substring(7,1) will return "2" in the name "Textbox2-1"

To access a specific textbox in a specific page use the indexofkey method:

Private Function GetTB(ByVal PageIndex As Integer, ByVal TBIndex As Integer) As TextBox
    Dim CurrentPage As TabPage = TabControl1.TabPages(TabControl1.TabPages.IndexOfKey("Repeat - " & PageIndex.ToString))
    GetTB = CurrentPage.Controls(CurrentPage.Controls.IndexOfKey("Textbox" & PageIndex.ToString & "-" & TBIndex.ToString))
End Function

With this function you pass the tabpage number and textbox number you're looking for and it will return the textbox

Then use it like this:

GetTB(2,3).Text = "some string"

The third textbox on the page named "Repeat - 2", will have the text "some string"

Thanks for Reply and Valuable advise for me,

An error comes when debug "InvalidArgument=Value of '-1' is not valid for 'index'. Parameter name: index"

Actually I need to put details from Access database rows/column data to this text boxes, while reading rows. It like this,

In database have few rows under sinlge person. (three column for each row)

Row Count = Tab Pages (with code its crated)

Then I want to add Access Column data of selected person to Text Boxes which we created on the Tab Pages
How I do that ?

An error comes when debug "InvalidArgument=Value of '-1' is not valid for 'index'. Parameter name: index"

Double check what numbers you're passing to the function.

Error comes when I try to manualy insert text to textboxes. Its oK when not call GetTB. may be it put it wrong place. I coded like this,

You gave me this code I entered as seperate sub,
then I call to GetTB from with Private Sub where that earlier codes writes.

    Private Function GetTB(ByVal PageIndex As Integer, ByVal TBIndex As Integer) As TextBox
    Dim CurrentPage As TabPage = TabControl1.TabPages(TabControl1.TabPages.IndexOfKey("Repeat - " & PageIndex.ToString))
    GetTB = CurrentPage.Controls(CurrentPage.Controls.IndexOfKey("Textbox" & PageIndex.ToString & "-" & TBIndex.ToString))
    End Function

Please Help to do I asked question from before post.

Like I said double check the numbers you are passing, also check to make sure the name strings are formatted exactly the same. For my function the tabpages "Repeat - 1", the textboxes, "Textbox1-1". If you're using my previous code there might a space missing for the textbox name.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.