Can I Add a form as a TabPage

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Visual Studio 2003 Windows Forms application:

Is there a way that I can add a form as a TabPage?

My goal is to develop a series of forms and load the dialogs dynamically in
a tab control based on the user selection of a particular function. I would
like to leverage the usefulness of the Form Designer to layout my dialogs and
then load them into TabPages.

I tried setting the TabPage as the Form.Parent and various other things; all
of which would either fail to compile or throw exceptions. I was unable to
find any samples that took this type of approach. I remember in the MFC
world that this was the standard way to develop tabbed dialos.

Any Ideas?

Thanks
 
I found a solution, but it seems like there should be a more elegant way to
do this. This is what I did. I created a TabControl in the form with a
single TabPage that contains all my controls. When I add a new TabPage in my
master form, I instantiate the child form and hand the master TabControl the
TabPage from my child form in the Add() method. I never call Show() on the
child form. Everything displays OK, but I haven't put any code or event
handling in yet to see what traps lie ahead with this.

My goal here is to be able to take advantage of the Form Designer to create
the tab pages that will number around 100 or more and load my TabPages at
runtime based on user input.

Thoughts?

Thanks
 
On Wed, 18 May 2005 19:52:05 -0700, Sleepy wrote:

Hi Sleepy
Is there a way that I can add a form as a TabPage?

My goal is to develop a series of forms and load the dialogs dynamically in
a tab control based on the user selection of a particular function. I would
like to leverage the usefulness of the Form Designer to layout my dialogs and
then load them into TabPages.

I tried setting the TabPage as the Form.Parent and various other things; all
of which would either fail to compile or throw exceptions. I was unable to
find any samples that took this type of approach. I remember in the MFC
world that this was the standard way to develop tabbed dialos.

I think the best way would be to do that with usercontrols. For each page
you create a usercontrol where you can put all the required controls to it
in the designer (same way as with forms) and then depending on the users
input you load the corresponding usercontrol in the tabpage.
 
Thanks Claudio, that is helpful. I had not used the UserControl before this.
This is much better that my first approach.

I see that I have to change the inheritance to TabPage from UserControl in
order to add it to the TabControl. This is fine except that the Designer
then messes up the layout if I open it in the designer while it is inherited
from TabControl.

I just saw a post warning of this and it just said to be careful to change
the inheritance back to UserContriol before entering into the Designer. I
think I can live with this but was just wondering if there is a smoother way
to manage this without having to toggle the inheritance.

Thanks again!
 
Thanks Claudio, that is helpful. I had not used the UserControl before this.
This is much better that my first approach.

I see that I have to change the inheritance to TabPage from UserControl in
order to add it to the TabControl. This is fine except that the Designer
then messes up the layout if I open it in the designer while it is inherited
from TabControl.

I just saw a post warning of this and it just said to be careful to change
the inheritance back to UserContriol before entering into the Designer. I
think I can live with this but was just wondering if there is a smoother way
to manage this without having to toggle the inheritance.

Thanks again!

I would recommend letting it always inherit from UserControl. And then you
add the usercontrols programmatically to the TabPages (with Dock=Fill).

Then you don't have to switch for and back with what to inherit from when
changing to the designer.
 
I see, that is much better.

So now I do this:

Project/Add New/User Control
Add my controls to the new UserControl
When it is time to dynamically add my tab page to the TabControl, I do the
following:

TabPage myTabPage = new TabPage(sometext)
myUserControl = new myUserControlType()
myUserControl.Dock = DockStyle.Fill
myTabPage.Controls.Add(myUserControl)
myTabControl.Add(myTabPage);

This works great, thanks again for your help!
 
Back
Top