Design/create panel control in a seperate class?

T

Tinus

Hello,

I've got a question and have been looking for an answer several days now....
no luck yet :-(
Hopefully someone here can provide me with an answer.

This is what I want to do:
(Using Visual Studio 2003 and language C#)
I created a standard Windows Form with two normal buttons (btn1 and btn2).
When I press btn1 I what to display a panel (with labels, button, etc. in
it) in the main form. But when I press btn2 I what to display a different
panel (with labels, text, etc. in it) on the same location as the previous
one.

For now, I've done this placing the two panel over each other and calling
BringToFront() to display them. But what if I whant to design 5 panels which
all need to be placed on the same location and are the same size....?

The best solution for me would be creating a seperate class for each panel
(layout and code). But I also want to use the Visual Studio designer to
design the panels.

Can somebody here tell me if this is possible?

Thanks in advance!

Tinus
 
S

Sean J Donovan

Tinus said:
Hello,

I've got a question and have been looking for an answer several days now....
no luck yet :-(
Hopefully someone here can provide me with an answer.

This is what I want to do:
(Using Visual Studio 2003 and language C#)
I created a standard Windows Form with two normal buttons (btn1 and btn2).
When I press btn1 I what to display a panel (with labels, button, etc. in
it) in the main form. But when I press btn2 I what to display a different
panel (with labels, text, etc. in it) on the same location as the previous
one.

For now, I've done this placing the two panel over each other and calling
BringToFront() to display them. But what if I whant to design 5 panels which
all need to be placed on the same location and are the same size....?

The best solution for me would be creating a seperate class for each panel
(layout and code). But I also want to use the Visual Studio designer to
design the panels.

Can somebody here tell me if this is possible?

Thanks in advance!

Tinus

Place a panel on the main form, use it as a place-holder.
Design all sub-panels separately, as UserControls (you could still
derive from Panel, but little point).
Just place the sub-panels within the place-holder panel (setting Dock.Fill).
Still use BringToFront to update the Z-order when required.

Voila.

Sean
 
T

Tinus

Thanks for your quick repley!

But how to I place the sub-panels in the place-holder panel?
Can you give me a code sample of how to do this (place the sub-panel in the
place-holder panel I mean)?

E.g. placeholderPanel is in frmMain.cs and subPanel is in ctlPanel.cs (which
is a UserControl)...
Now what code do I use in frmMain to display the subPanel which is defined
in ctlPanel.cs in the placeholderPanel?

Thanks again!

Tinus
 
S

Sean J Donovan

OK, on your main-form, you'll have a panel

private Panel _myPanel

Design your user-controls (eg. UserControl1), to add to the main on the
main-form, code (not design) the following in your button handler:

// Instantiate the user control to display in the panel.
UserControl1 uc = new UserControl1();

// Set the user-control's dock-style to Fill .
uc.Dock = DockStyle.Fill;

// Add the control to your place-holder panel.
_myPanel.Controls.Add( uc );

Obviously, you'll need to add logic to check to see whether you'd
already created the control. You could record record the variable in
your form, or just iterate through the controls and look for it. The
above might become:

// Within the button handler . . .

// Search the panel for a user-control of the desired type.
// If a user-control of the right type was found, bump it to
// the top of the Z-order.
foreach( Control c in _myPanel.Controls )
{
if ( c is UserControl1 )
{
c.BringToFront();
return;
}
}

// OK, the control of that type wasn't found, go create one.

// IMPLEMENT THE CODE AT THE TOP.

There are a number of ways to do this, this just gives you an idea of
what to do.

Sean
 
S

Sean J Donovan

I guess the other option is to use the TabControl, which sounds kinda'
close to what you're trying to do.
 

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