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

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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Instead of trying to dynamically read the contents of the panel and recreate them...you 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...
GarySBAuthor Commented:
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
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

GarySBAuthor Commented:
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?:
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Well...you 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
GarySBAuthor Commented:
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

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.