Tab control with many pages

P

p19010101

I'm working on a single form app, the form contains a tab control with
multiple pages. As a result the code gets very long in the code page,
which can be a pain to maintain, basically it's a mess.

What I'm planning to do is to break down the codes into separate
modules, i.e. one module per tab page, and pass the controls to the
module's subroutines as parameters. It should work but I think it's
wrong, or at least not the .net way of doing things.

Perhaps I should make each tab page as a class, then attach them to the
tab control. But I have little idea how to do that. Do I create user
controls then drop them into the tab pages, or should I derive my own
class from System.Windows.Forms.TabPage?

Please help, simple code snippets would be appreciated. TIA.
 
P

p19010101

Ok, I think I've got it working, so far so good. But...I don't know if
it's the right way or would it cause problems down the track. Following
is what I'm doing:

1. create a number of forms, one of them is the main form and contains
the tab control

2. add some tab pages to the tab control in the main form

3. add other controls into the rest of the forms (the forms are
essentially the tab pages)

4. change "Inherits System.Windows.Forms.Form" to "Inherits
System.Windows.Forms.TabPage" in the forms

5. comment out "Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)"
in the forms

6. in the main form, change "Me.TabPage1 =
System.Windows.Forms.TabPage" to "Me.TabPage1 = New Form2", tabpage2 =
form3, tabpage3 = form4, and so on...

Change the forms to System.Windows.Forms.TabPage loses the design view,
so I have to change it back to System.Windows.Forms.Form and uncomment
Me.AutoScaleBaseSize when I need to edit the layout at design time.

What do you think? Is this correct or awfully wrong?
 
M

Mick Doherty

I would use UserControls rather than Forms.

One way to do exactly what you want is to create a UserControl for each
TabPage.
Set the Tag property of each tabpage to the name of the UserControl you
created specifically for it.

Then simply add the following code to your form (assumes your tabcontrol is
named TabControl1):

\\\
Private Sub MainForm_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
For Each tp As TabPage In Me.TabControl1.TabPages
If Not (tp.Tag Is Nothing) Then
Dim ctrl As Control = CreateTabChild(tp.Tag.ToString)
If Not (ctrl Is Nothing) Then
ctrl.Dock = DockStyle.Fill
tp.Controls.Add(ctrl)
End If
End If
Next
End Sub

Private Function CreateTabChild(ByVal ctrlName As String) As Control
Dim assyName As String = Me.GetType.Assembly.GetName.Name
Dim assy As Reflection.[Assembly] = _
Reflection.[Assembly].LoadWithPartialName(assyName)
Return CType(assy.CreateInstance(assyName + "." + ctrlName), Control)
End Function
///
 
M

Mick Doherty

In fact I'm thinking far too hard ;-)

Just drop the usercontrol onto the tabpage at designtime and set it's dock
property to Fill.

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html


"Mick Doherty"
I would use UserControls rather than Forms.

One way to do exactly what you want is to create a UserControl for each
TabPage.
Set the Tag property of each tabpage to the name of the UserControl you
created specifically for it.

Then simply add the following code to your form (assumes your tabcontrol
is named TabControl1):

\\\
Private Sub MainForm_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles
MyBase.Load
For Each tp As TabPage In Me.TabControl1.TabPages
If Not (tp.Tag Is Nothing) Then
Dim ctrl As Control = CreateTabChild(tp.Tag.ToString)
If Not (ctrl Is Nothing) Then
ctrl.Dock = DockStyle.Fill
tp.Controls.Add(ctrl)
End If
End If
Next
End Sub

Private Function CreateTabChild(ByVal ctrlName As String) As Control
Dim assyName As String = Me.GetType.Assembly.GetName.Name
Dim assy As Reflection.[Assembly] = _
Reflection.[Assembly].LoadWithPartialName(assyName)
Return CType(assy.CreateInstance(assyName + "." + ctrlName), Control)
End Function
///

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html


Ok, I think I've got it working, so far so good. But...I don't know if
it's the right way or would it cause problems down the track. Following
is what I'm doing:

1. create a number of forms, one of them is the main form and contains
the tab control

2. add some tab pages to the tab control in the main form

3. add other controls into the rest of the forms (the forms are
essentially the tab pages)

4. change "Inherits System.Windows.Forms.Form" to "Inherits
System.Windows.Forms.TabPage" in the forms

5. comment out "Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)"
in the forms

6. in the main form, change "Me.TabPage1 =
System.Windows.Forms.TabPage" to "Me.TabPage1 = New Form2", tabpage2 =
form3, tabpage3 = form4, and so on...

Change the forms to System.Windows.Forms.TabPage loses the design view,
so I have to change it back to System.Windows.Forms.Form and uncomment
Me.AutoScaleBaseSize when I need to edit the layout at design time.

What do you think? Is this correct or awfully wrong?
 
P

p19010101

Thanks, I'm taking your suggestion.

I noticed if I change System.Windows.Forms.UserControl to
System.Windows.Forms.TabPage then the TabControl can reference it
directly, but I will lose the design view. Oh well, I guess I'd stick
to the UserControl. Cheers.
 
D

dgk

I'm working on a single form app, the form contains a tab control with
multiple pages. As a result the code gets very long in the code page,
which can be a pain to maintain, basically it's a mess.

What I'm planning to do is to break down the codes into separate
modules, i.e. one module per tab page, and pass the controls to the
module's subroutines as parameters. It should work but I think it's
wrong, or at least not the .net way of doing things.

Perhaps I should make each tab page as a class, then attach them to the
tab control. But I have little idea how to do that. Do I create user
controls then drop them into the tab pages, or should I derive my own
class from System.Windows.Forms.TabPage?

Please help, simple code snippets would be appreciated. TIA.


Just a guess here but wouldn't partial classes be a way to go? By
default all the code would go into the form's main code module but
couldn't you move it to another module so long as it has the same
class name? VS2005 of course.
 

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