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

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
  • 4
  • 3
1 Solution
Mike TomlinsonMiddle School Assistant TeacherCommented:
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 TomlinsonMiddle School Assistant TeacherCommented:
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

Industry Leaders: 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!

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 TomlinsonMiddle School Assistant TeacherCommented:
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 TomlinsonMiddle School Assistant TeacherCommented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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