tabpage clone

T

touf

Hi,
I've a tabcontrol that contains many similar tabpages (exactly the same
structure with different information), the tabpages number isn't known in
the design time it depends of the data.
Is there a way to clone ( copy the structure) a tabpage to create anew one
by code?
thanks.
 
F

Fergus Cooney

Hi Touf,

|| Is there a way to clone (copy the structure) a tabpage to
|| create a new one by code?

Can you tell me what controls you have on your TabPages? Also, what,
specifically, do you want to do with the new TabPage?

Regards,
Fergus
 
T

touf

thanks Fergus
I like to display statistic for cutomers
for each customer I create a new TabPage that contain :
- a label and a textBox that contains the customer name
- a listBox that contain the total of the sales by year.

I've created the first TabPage in the design view, and I like to create the
others by code.
 
A

Armin Zingler

touf said:
thanks Fergus
I like to display statistic for cutomers
for each customer I create a new TabPage that contain :
- a label and a textBox that contains the customer name
- a listBox that contain the total of the sales by year.

I've created the first TabPage in the design view, and I like to
create the others by code.

I'd create them all by code. I'd probably derive my own class from Tabpage
and create a new instance per customer. To save some work, you could move
the code, generated by the designer for your already existing tabpage, to
you derived TabPage.
 
F

Fergus Cooney

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>
 
V

Versteijn

touf said:
Hi,
I've a tabcontrol that contains many similar tabpages (exactly the same
structure with different information), the tabpages number isn't known in
the design time it depends of the data.
Is there a way to clone ( copy the structure) a tabpage to create anew one
by code?
thanks.

One option is to create a custom control (class), i.e. your tabpage
with the needed controls on it.

Good luck

Freek Versteijn
 
F

Fergus Cooney

Hi Touf, Freek,

And a damn fine suggestion , too! For if you have a UserControl and change
the layout or what-have-you, it will work for all of the TabPages. Whereas, if
you go the make-a-copy route that I showed you earlier, you will have more
work to do every time your design changes. It depends on which is easier and
how stable your UI design is.

Regards,
Fergus
 
T

touf

Thans friends,
I think that the best way I have, is to move the code of the designer to a
sub and call it with parameters...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top