Create tabs, copy controls from base tab to new one

jbmckim

New member
Joined
Sep 3, 2009
Messages
4
Programming Experience
10+
I have a tab (created in the form developer) that contains about a dozen controls. I need to be able to clone this tab w/all controls. I have no problem creating the forms. However, the controls don't copy. This is true whether I create the new tab with a string:


... TabPages.Add(MO.Name)

Or with a tab object assigned to the base form:

Dim NewTab As New TabPage
NewTab = MyBaseTabPage
... TabPages.Add(NewTab)


In either case, my first tab has everything it should have the other tabs have, the other tabs have only the tab name.

Basically, I want to be able to clone my base tab into n number of tabs.

Thanks.
 
That's really not the way to go about it. What you should do is either:

1. Define your own class that inherits TabPage and, it's constructor, create all the controls, configure them and add them to the container. Now, when you use that class in code, you simply create an instance and all the controls are just there. You can also add extra properties and methods to the class to provide controlled access to the aspects of the controls you need to manipulate without having to make the controls public.

OR

2. Design a UserControl that contains all your child controls and, at run time, create a regular TabPage and add an instance of your UserControl.

The second option might be easier because it lets you see the controls in the designer, both in the UserControl and in the form, while the first option requires you to do everything in code.
 
I'm going to give #2 a crack. That does seem to be easier as it will let me more easily re-use a lot of the prototyping I've done to this point.

I'll get back in a day or so to report on the finished version.

It's interesting to me that my approach didn't work. I would think I'd either be able to implicitly inherit in the manner I outlined or that it would cause a compile time problem. However, I've been doing this long enough that understanding this is mildly interesting as opposed to wildly important.
 
Had it working but needed to restructure some...so of course it broke. The following code builds the tabs correctly but doesn't add the controls. Any ideas?

Thanks.

Amendment - the last tab properly loads the user control, all the preceding are blank.

VB.NET:
Public Class Spectrometer_Setup_Form

    Private Sub Spectrometer_Setup_Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim MO As MonitorClass
        Dim SpecTab As New SpecTabControls

        For Each MO In NVSpecMon.MonitorCol
            SpectrometerSetup.TabPages.Add(MO.Name)
            SpectrometerSetup.TabPages(MO.iSpecNumber).Controls.Add(SpecTab)
        Next


    End Sub

End Class
 
Last edited:
You only made 1 set of controls (Dim SpecTab As New SpecTabControls)
where as the loop is adding several right?
Try this:
VB.NET:
   For Each MO As MonitorClass In NVSpecMon.MonitorCol
         Dim SpecTab As New SpecTabControls
         SpectrometerSetup.TabPages.Add(MO.Name)
         SpectrometerSetup.TabPages(MO.iSpecNumber).Controls.Add(SpecTab)
   Next
 
Tab cloning

Yup, that's it. And of course, that's the way I had it before I restructured the class stuff a bit. Didn't catch my own madness.

Most of my experience is with c++. Jiggling the class and scope "wires" to get stuff to work doesn't seem as subtle to me there as it does in this environment. This is good though.

Thanks to both you guys. I'll summarize:

Want to clone a tab n times?

1) Create 1 blank tab. (Not strictly necessary but somewhat easier - VB can get confused...or at least the programmer can.)

2) Create a User Control that contains everything you want on the tab. (Very easy. Google it or see O'Reilly's VB 2005 Cookbook or other)

3) In the appropriate Load routine, add your version of:

VB.NET:
        Dim MO As MonitorClass

        For Each MO In NVSpecMon.MonitorCol
            Dim SpecTab As New SpecControl
            If (MO.iSpecNumber = 0) Then
                SpectrometerSetup.TabPages(MO.iSpecNumber).Text = MO.Name 'Renames the first tab which is used in the form view
            Else
                SpectrometerSetup.TabPages.Add(MO.Name) 'Builds n tabs after the first
            End If

            SpectrometerSetup.TabPages(MO.iSpecNumber).Controls.Add(SpecTab) 'Adds your homemade User Control to the whole mess
        Next
 
Back
Top