Hi, i have a project to generate code for TabControl.
I'm doing 1 form whit textbox-es to enter the tabcontrol properties (name, size, pozition ...) For the number of the tab pages in the control i'm generating dynamicly another tab control with the number of tab pages entered in the tab control tab pages field

Public Sub TextBox_number_pages_TextChanged.....
Dim tab_pages As TabPage
Dim n As Integer = Convert.ToInt32(TextBox_number_pages.Text)
        For i As Integer = 0 To n

Dim TextBox_tp_name As New TextBox
            TextBox_tp_name.Text = ""
            TextBox_tp_name.Location = New Point(220, 30)
tab_pages = New TabPage()
            tab_pages.Parent = TabControl1
            tab_pages.Name = "TabPage " + Convert.ToString(i + 1)
            tab_pages.Text = tab_pages.Name
tab_pages.Controls.Add(TextBox_tp_name)
TabControl1.Controls.Add(tab_pages)

this is the part of the code, i have more fields.
What i'm trying to do is when i enter the tab name in theTextBox_tp_name field in the generated dynamicly tab pages, the tab_pages.Text have to change when i enter a new name in the theTextBox_tp_name

I thing that i can't explain exactly what i'm trying to do but i can't explain it without posting the whole code.

Recommended Answers

All 12 Replies

Sounds to me you want to have a TextChanged event wired to TextBox_tp_name.

You can try declaring TextBox_tp_name globally using WithEvents, which lets you write event handling code for it. You can also use the AddHandler statement to wire an event handler to a locally declared version of Textbox_tp_name instead.

I'm not entirely sure how well those would work, but they're worth experimenting with. :)

Ok, so i made a simple demonstration of what i want to do:
Form1 has one TextBox and one TabControl withowt TabPages. I generate the TabPages dynamically in the next code.
Here is the code in form1.vb
What i need is to transfer the TextBox_tp_name.Text to the TextBox1.Text in the Form1

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim tab_pages As TabPage
        For i As Integer = 0 To 2
            Dim TextBox_tp_name As New TextBox
            TextBox_tp_name.Text = ""
            TextBox_tp_name.Location = New Point(220, 30)
            Dim Label_tp_name As New Label
            Label_tp_name.Text = "Name"
            Label_tp_name.Location = New Point(10, 40)
            tab_pages = New TabPage()
            tab_pages.Name = "Page " + Convert.ToString(i + 1)
            tab_pages.Text = tab_pages.Name
            tab_pages.Controls.Add(TextBox_tp_name)
            tab_pages.Controls.Add(Label_tp_name)
            TabControl1.Controls.Add(tab_pages)
        Next
    End Sub
    Private Sub Botton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        TextBox1.Text = Me.TextBox_tp_name.Text
    End Sub
End Class

Try something like this:

'add textbox to page
        'ensure control is named as you will retrieve it by name
        Dim tb As New TextBox
        tb.Name = "tb1"
        Dim tabPage As New TabPage
        tabPage.Controls.Add(tb)


        'retrieve control from tabpage
        Dim getTB As TextBox
        getTB = CType(tabPage.Controls.Find("tb1", False)(0), TextBox)

Try something like this:

'add textbox to page
        'ensure control is named as you will retrieve it by name
        Dim tb As New TextBox
        tb.Name = "tb1"
        Dim tabPage As New TabPage
        tabPage.Controls.Add(tb)


        'retrieve control from tabpage
        Dim getTB As TextBox
        getTB = CType(tabPage.Controls.Find("tb1", False)(0), TextBox)

I've tried it but it doesn't work, when i push the button nothing happens

how did you implement my code? it wasnt intended as a complete method, they are two seperate examples. One shows hwo to give the textbox a name when you add it, the other shows how to retrieve a reference to a textbox by name. Once you have a reference to it you can access its members: Textbox1.Text = getTB.Text Post your code so far and i'll see if you have used my code in the right place

OK, here is the whole code

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim tab_pages As TabPage
        For i As Integer = 0 To 2

            Dim TextBox_tp_name As New TextBox()
            TextBox_tp_name.Name = "tb"
            TextBox_tp_name.Location = New Point(220, 30)

            tab_pages = New TabPage()
            tab_pages.Name = "Page" + Convert.ToString(i + 1)
            tab_pages.Text = tab_pages.Name

            tab_pages.Controls.Add(TextBox_tp_name)
            TabControl1.TabPages.Add(tab_pages)
        Next
    End Sub
    Private Sub Botton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim getTB As TextBox
        getTB = CType(tab_pages.Controls.Find("tb", False)(0), TextBox)
        TextBox1.Text = getTB.Text
    End Sub
End Class

The problem here is that tab_pages only exists within the Form1_Load method. You need to first retrieve the tabpage from the tabcontrol:

'retrieve control from tabpage
        Dim getPage As TabPage
        getPage = TabControl1.TabPages("Page1")
        Dim getTB As TextBox
        getTB = CType(getPage.Controls.Find("tb1", False)(0), TextBox)

Also, you are naming all of your textboxes "tb" you need o use the same method you used to name your tabPages: TextBox_tp_name.Name = "tb" + Convert.ToString(i + 1)

Now i have this code in Button1_Click:

Private Sub Botton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim getPage As TabPage
        getPage = TabControl1.TabPages("Page1")
        Dim getTB As TextBox
        getTB = CType(getPage.Controls.Find("tb1", False)(0), TextBox)
        TextBox1.Text = getTB.Text
    End Sub

I also made the TextBox_tp_name.Name = "tb" + Convert.ToString(i + 1)
but it doesn't work :(

ok im totally confused here.
You have a form (called Form1) that contains one textbox and one tabcontrol.
You want to transfer the content of a textbox from another Form to the textbox that is in Form1?
please tell us more detailed what exactlly your problem is and what controls are placed where, as (atleast me) dont get the meaning of your code at all :s

I have one form Form1 with 1 textbox, 1 button and 1 tabcontrol without tabpages. In the Form1_Load i create dynamically the pages of the tabcontrol with 1 textbox in every page. I want when i click the button to transfer the content of the textbox in the tabpage1 for example in the textbox outside the tabcontrol.
That's it.

someone help me please ....

ok, i fixed it, it finally work with the Ryshad last Post
Thanks !

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.