Hi Touf,
There is nothing in .NET to clone a Control and its children so it all has
to be done 'manually'. It <is> possible to do it with Reflection and recursion
but I think that would be overkill in this case.Thankfully, it sounds as if
your TabPages aren't too complicated.
What I recommend is that you look in the Windows Form Designer code
section and simply copy out the parts that created your first TabPage. Stick
these into a separate routine and add the appropriate parameters for things
like label captions, etc..
The event handling has to be added explicitly using AddHandler. With a
Designer-added Control, event handling is done for you, so you get:
Sub Button1_Click (sender, e) Handles Button1.Click.
You'll need to do
AddHandler ButtonX.Click, AddressOf Button1_Click
This will make Button1 and ButtonX go to the same routine for their
Clicks, so you may need to determine which one is being pressed - this will be
the sender argument.
Have a look at the example below which creates a new TabPage with a Label,
a TextBox and a Button. Note how these controls are added to the TabPage and
then the TabPage is added to the TabControl.
SuspendLayout() and ResumeLayout() are in comments. They prevent
flickering by stopping the TabControl from drawing itself unti the new
Tabpages have been added. You may not need to call these but if you do, that's
where they go.
Come back if you need further help with this.
Regards,
Fergus
<code>
Private Sub AddTabPage (... <Text values, etc> ...)
Dim LabelX As New Label
Dim TextBoxX As New TextBox
Dim ButtonX As New Button
'A new Label
LabelX.Location = New System.Drawing.Point(39, 56)
LabelX.Name = "LabelX"
LabelX.TabIndex = 4
LabelX.Text = "LabelX"
'And a new TextBox
TextBoxX.Location = New System.Drawing.Point(31, 20)
TextBoxX.Name = "TextBoxX"
TextBoxX.TabIndex = 3
TextBoxX.Text = "TextBoxX"
'And a new Button
ButtonX.Location = New System.Drawing.Point(159, 28)
ButtonX.Name = "ButtonX"
ButtonX.TabIndex = 5
ButtonX.Text = "ButtonX"
AddHandler ButtonX.Click, AddressOf Button1_Click
'And a new TabPage to put them on.
Dim TabPageX As New TabPage
TabPageX.Location = New System.Drawing.Point(4, 22)
TabPageX.Name = "TabPageX"
TabPageX.Size = New System.Drawing.Size(264, 98)
TabPageX.TabIndex = 0
TabPageX.Text = "TabPageX"
'Add the controls to the TabPage.
TabPageX.Controls.AddRange _
(Control() {LabelX, TextBoxX, ButtonX})
'Add the TabPage to the TabControl.
'Me.TabControl1.SuspendLayout()
Me.TabControl1.Controls.Add (TabPageX)
'Me.TabControl1.ResumeLayout(False)
'Bring the new TabPage to the front.
TabControl1.SelectedTab = TabPageX
End Sub
</code>