• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1304
  • Last Modified:

Copy Everything from TabPage1 to tabpage2

I have a form where users can click a button to add another tab while creating a duplicate of the controls and data on TabPage1.
I read somewhere about parent controls, but not sure if this is the way to go or how to do it.
Would you please provide a code sample, my IE crashes when I click on links.
  • 6
  • 3
1 Solution
I would go down the route of a dynamic array of controls.

you can then redefine the array (not forgetting the PRESERVE keyword to keep the others in tact with there data)
i.e something like
Dim mytextbox() As Control
dim x as integer

Private Sub Form_Load()

'Get the next array index

'Create the next element (while preserving the ones already in the array)
redim preserve mytextbox(x)

Set mytextbox(x) = Form1.controls.Add("VB.textbox", "mytextbox", Form1)

this will allow you to acces the textboxes as follows

mytextbox(0).text = "this was the first"
mytextbox(1).text = "this was the second"
or you can use a variable to specify which text box i.e

mytextbox(x).text = "This is array element number " & x & "out of" & ubound(mytextbox())

hope this helps
SheritlwAuthor Commented:
I read about creating a user control and then I can use it over and over.  How would I do that?
Here is some info on creating user controls for you


However you will still need to create an array of the control to be able to indentify them in code. As you cannot reference a control using a variable in code you will need an array to be able to use a variable to reference the array position. I will try to explain slightly more with some examples.

dim uControl as yourUserControl
you will only be able to use uControl if you duplicate it you cannot refer to it in your code

dim str as controlstring
str = "uControl"

str.text 'WILL NOT point to the control it will point to the str variable.

therefore you must array it so you can use variables in your code.

dim uControl() as control
dim x as integer

redim preserve uControl(1)
x = 1
uControl(x) = "Whatever"
redim preserve uControl(2)
uControl(x) = "this is control 2"
uControl(x-1) = "this is control 1"

Therefore you can use user controls but to dynamically create them on the fly you must use them as an Array. Otherwise you would have to define them all first individually with dim statements and you would put immediate limitations on maximums and end up with very very messy code trying to refer to the correct controls.

The array method is the only feasable option.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

SheritlwAuthor Commented:
I am making the user control now.  Should I create a class in the Windows Control Library and put all the code for the components in it?
If you are going to be around, I will be asking a lot of questions as I go along with this.  You might as well get the points... 500 for each question.
I see what you are getting at a bit more now

you want to make a class which is your tab with all its components on

you want an array of this tab so you can duplicate it containing all its components.


you want to be able to duplicate the lot at a theoretically infinite number of times. therefore

you want an array of the tab containg everything yes?
then you can redimension the array and duplicate the lot without having to duplicate each item.

you can then refer to them incode as
tab(0).textbox1 = "this is textbox 1 on tab 0"
tab(1).textbox1 = "this is texbox 1 on tab 1"
tab(2).textbox1 = "this is textbox 1 on tab 2"

am I understanding you correctly?

which vb are you using?

as vb6 uses arrays
vb net doesn't have arrays but does have some alternatives
SheritlwAuthor Commented:
I tried to make a user control, but I only have a few more days to have the copying of the tab pages, with splitcontainer and controls over to a new tab page working.
Is there a way I could copy the designers creation of the controls over to a class and use that as a template to create the new tab with all the controls?
I thought I could rename the controls by appending a number after the name of each control to create the new names of the controls.  The numbers would match the tab index.
Is this possible and if yes, how would I set up the class?
Have a look at the example I made

It has a blank form

it creates a tab page dynamically
it creates a panel dynamically and adds to tab page
it creates 2 text boxes dynamically and adds to the panel

then it adds another tab page and continues 5 times

hope this helps

I have put the project in a rar file but had to append a .txt extension to be able to upload so remove the .txt extension extract the rar open the project with Visual Basic 2008.

the form starts completley blank as all controls are built dynamically

code reads as follows for those who dont want to download the example

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim tabCtrl As TabControl
        Dim container(0) As Panel
        Dim boxes(1) As TextBox
        Dim i As Integer
        Dim i2 As Integer
        Dim i3 As Integer

        tabCtrl = New TabControl
        tabCtrl.Width = 384
        tabCtrl.Height = 402
        tabCtrl.Left = 12
        tabCtrl.Top = 24

        For i3 = 0 To 5

            tabCtrl.TabPages.Add("Tab " & i3)

            For i2 = 0 To 1

                ReDim Preserve container(i2)

                container(i2) = New Panel
                container(i2).Width = 296
                container(i2).Height = 141
                container(i2).Left = 12
                container(i2).Top = 12 + (i2 * 150)
                container(i2).BorderStyle = BorderStyle.Fixed3D

                For i = 0 To 1
                    boxes(i) = New TextBox()
                    boxes(i).Width = 200
                    boxes(i).Height = 25
                    boxes(i).Left = 10
                    boxes(i).Top = i * 30

    End Sub
End Class

You will be able to refer to your textboxes (or whatever controls you use of course) similar to the following

dim tabnumber as integer
dim containernumber as integer
dim textboxnumber as integer

tabnumber = 0
containernumber = 0
textboxnumber = 1

tabctrl.tabpages(tabnumber).container(containernumber).boxes(textboxnumber).text = "This is tab " & tabnumber & " ,panel " & containernumber & " ,textbox " & textboxnumber

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now