I want to duplicate a Panel in vb 2008 n number of times, each on its own Tab on a Tabcontrol

Posted on 2009-02-19
Last Modified: 2013-12-25
I have one main Panel on a form that displays labels, textbox's, picturebox and a tablelayoutpanel (tablelayoutpanel contains: labels, textbox's and checkbox).  When the user clicks a button on the Panel, I want to place a copy of the Panel into my TabControl without loosing controls from the original Panel.  If they click the button five times I should have five independent working Panels in my Tabcontrol.

I have some code that partially works, but it zaps the controls off of my original Panel.  Thank youf for you assistance

 Dim ctrl As Control
        For Each ctrl In Me.PDF_Panel.Controls
            If Not IsNothing(ctrl) Then
                Dim tmpCtrl As New Control
                tmpCtrl = ctrl
                My_New_Tab_Panel(BOM_TabControl1.TabPages.Count + 1 - 2).Controls.Add(tmpCtrl)
            End If
Question by:GarySB
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Instead of trying to dynamically read the contents of the panel and recreate should instead either create a UserControl or a Borderless Form that has those controls on it.  Then you can simply create a new instance of it and add it to the TabPage...

    Author Comment

    Your saying that I should move all the controls on my main Panel to a new Form and delete the Panel?
    And then I will be able to copy the form to a TabPage?
    Can you please give me an example line of code using a new instance to accomplish, Thank You

    Code I use to make a new Tab:
    Dim myTabPage As New TabPage()
            myTabPage.Text = "RFQ" & (BOM_TabControl1.TabPages.Count + 1 - 2)
            AddHandler BOM_TabControl1.Click, AddressOf BOM_TabControl1_Click
            AddHandler BOM_TabControl1.MouseUp, AddressOf BOM_TabControl1_MouseUp
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    You can still use the Panel to house the Form.

    To show a form in a TabPage you first have to set the TopLevel() Property to false:
    Dim frm As New Form2 ' ...whatever your form name is...
    frm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    frm.Dock = DockStyle.Fill
    frm.TopLevel = False ' <--- This is KEY!!!

    Open in new window


    Author Comment

    Thank you for your help.
    I understand the code you wrote and I am able to add the form at runtime directly to the tab or the panel.

    Since the form is being created at runtime, wont I have the same problem of transfering the controls from the main panel to the form?:
    LVL 85

    Expert Comment

    by:Mike Tomlinson still need to copy the VALUES...but you won't need to re-create all the controls anymore.

    It might look something like:

        ' ...create new instance of Form "frm"...
        frm.TextBox1.Text = Me.TextBox1.Text
        frm.TextBox2.Text = Me.TextBox2.Text

    Author Comment

    Last thing, I must be missing something because your saying i do not need to cre-create the controls on the form, but when I place the code above with my textbox names it reports that the textbox's are not a member of the form. Thank You

    LVL 85

    Accepted Solution

    You're not "missing" anything...I just can't give you exact code because I don't have your exact setup in front of me!...

    You can either:

    (1) Keep ONE of the Forms in your main panel.  Then create a new instance of the form and copy the values into it.  Finally, place the instance in the TabPage.
    (2) MOVE the Form from the Panel to your TabPage and place a new, blank instance back in the Main Panel.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Suggested Solutions

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    746 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

    15 Experts available now in Live!

    Get 1:1 Help Now