Hi Ronald,
There are different ways to do this. One way is to treat your user controls
as plugins. Example would be that all your controls implement an interface
which would have Load(...) and Unload(...) methods. Then you would have a
control container for each option which would contain the user controls for
that option. And finally when ever a user selects an option then the form
would call Controlcontaine r's Display() method (Check the code below)
some pseudo-code:
public interface IControlPlugin
{
void Load(...);
void Unload(...);
}
public class MyControl : UserControl, IControlPlugin
{
...
public void Load(...)
{
...
}
public void Unload(...)
{
...
}
}
public class ControlContaine r : UserControl
{
private List<UserContro lmControls;
public void Add(UserControl control)
{
if(control is IControlPlugin)
mControls.Add(c ontrol);
else
throw new Exception("Inte rface not implemented");
}
public void DisplayControls (Control parent)
{
foreach(UserCon trol ctrl in mControls)
{
IControlPlugin plugin = ctrl as IControlPlugin;
ctrl.Load();
parent.Controls .Add(ctrl);
ctrl.Parent = this;
}
}
public void UnloadControls( Control parent)
{
foreach(UserCon trol ctrl in mControls)
{
IControlPlugin plugin = ctrl as IControlPlugin;
ctrl.Unload();
parent.Controls .Remove(ctrl);
ctrl.Parent = null;
ctrl.Dispose();
}
}
}
Hope this helps
Fitim Skenderi
"Ronald S. Cook" <rc***@westinis .comwrote in message
news:%2******** **********@TK2M SFTNGP06.phx.gb l...
We have a Windows app that has one main form (a shell, sort of). We then
load user controls into a panel on the form depending on what the user has
selected.
Our current code to unload the existing user control and load the newly
selected one is pretty bulky. Every time we add a new user control to the
project, we have to add some code in the section where we are
loading/unloading.
Is there a more dynamic, more efficient way to manage user controls
loading in a form?
Any help would be appreciated.
Thanks,
Ron