Dynamic views

J

Jesper DK

Hi,

I'm writing a windows explorer like application. The GUI
is divided by a vertical splitter. To the left I have a
tree control. To the right of the splitter I would like
to display one of several different user controls
according to what is selected in the tree control. Now, I
can do this by placing all of my different user controls
on the form and then use .Show() and .Hide() on the
controls. However, I do not fancy this technique, Isn't
there a more elegant way of doing this.

Another thing, If I instantiate these user controls
dynamically and place them on the form, should they be
placed on a panel?. Where should I store the reference to
the user control.

Thank you in advance.
Best regards Jesper.
 
N

Noah Coad [MVP .NET/C#]

Hey Jesper,

I do this exact same thing in several of my programs. There is a list of
'commands', each has its own GUI interface so there is a spot where the
correct control plugs in. Here are some things to consider:

1. It would be a good idea, but not required, to make one UserControl
control that is a basic version of the plug-in control. Then the specific
plug-ins can all be extended from your 'template' instead of UserControl.
This gives you more control over how they look and interact.

2. Yes, put a panel where you want the controls to go, it makes things a
little easier. Let's call it 'pnlHost'. All you really need to do is look
at the Windows Designer Generate Code to see how controls are added via
code. I'll give you what you need here.

private void ShowControl(UserControl c)
{
pnlHost.Controls.Clear(); // Remove any current controls in the panel
pnlHost.Controls.Add(c); // Put the special control in the host panel
c.Dock = DockStyle.Fill; // Make the control fill the host panel
}

Then say you have a custom control called "PageSettings", you want to show
it in the host panel, just call: ShowControl(new PageSettings());

That's practically all there is to it!

Good luck!

-Noah Coad
Microsoft MVP & MCP [.NET/C#]
 
J

Jesper DK

Thanks Noah

Could you clarify point 1. Provide a link to a sample
perhaps. I would like to learn to do it right.

Thanks and best regards.
Jesper
-----Original Message-----
Hey Jesper,

I do this exact same thing in several of my programs. There is a list of
'commands', each has its own GUI interface so there is a spot where the
correct control plugs in. Here are some things to consider:

1. It would be a good idea, but not required, to make one UserControl
control that is a basic version of the plug-in control. Then the specific
plug-ins can all be extended from your 'template' instead of UserControl.
This gives you more control over how they look and interact.

2. Yes, put a panel where you want the controls to go, it makes things a
little easier. Let's call it 'pnlHost'. All you really need to do is look
at the Windows Designer Generate Code to see how controls are added via
code. I'll give you what you need here.

private void ShowControl(UserControl c)
{
pnlHost.Controls.Clear(); // Remove any current controls in the panel
pnlHost.Controls.Add(c); // Put the special control in the host panel
c.Dock = DockStyle.Fill; // Make the control fill the host panel
}

Then say you have a custom control
called "PageSettings", you want to show
it in the host panel, just call: ShowControl(new PageSettings());

That's practically all there is to it!

Good luck!

-Noah Coad
Microsoft MVP & MCP [.NET/C#]



Jesper DK said:
Hi,

I'm writing a windows explorer like application. The GUI
is divided by a vertical splitter. To the left I have a
tree control. To the right of the splitter I would like
to display one of several different user controls
according to what is selected in the tree control. Now, I
can do this by placing all of my different user controls
on the form and then use .Show() and .Hide() on the
controls. However, I do not fancy this technique, Isn't
there a more elegant way of doing this.

Another thing, If I instantiate these user controls
dynamically and place them on the form, should they be
placed on a panel?. Where should I store the reference to
the user control.

Thank you in advance.
Best regards Jesper.


.
 

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