Copy Everything from TabPage1 to tabpage2

Posted on 2009-04-18
Last Modified: 2012-05-06
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.
Question by:Sheritlw
    LVL 4

    Expert Comment

    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

    Author Comment

    I read about creating a user control and then I can use it over and over.  How would I do that?
    LVL 4

    Expert Comment

    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.

    Author Comment

    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.
    LVL 4

    Expert Comment

    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?

    LVL 4

    Expert Comment

    which vb are you using?

    as vb6 uses arrays
    vb net doesn't have arrays but does have some alternatives

    Author Comment

    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?
    LVL 4

    Accepted Solution

    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

    LVL 4

    Expert Comment

    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

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Suggested Solutions

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now