Re: Need more efficient way to load/unload user controls on form
- From: "Ronald S. Cook" <rcook@xxxxxxxxxxxx>
- Date: Wed, 14 Feb 2007 07:20:45 -0700
Thanks, I'll try that. Any other options?
"fitim skenderi" <fitims@xxxxxxxxxxx> wrote in message
news:O46QY9BUHHA.2124@xxxxxxxxxxxxxxxxxxxxxxx
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 Controlcontainer'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 ControlContainer : UserControl
{
private List<UserControl> mControls;
public void Add(UserControl control)
{
if(control is IControlPlugin)
mControls.Add(control);
else
throw new Exception("Interface not implemented");
}
public void DisplayControls(Control parent)
{
foreach(UserControl ctrl in mControls)
{
IControlPlugin plugin = ctrl as IControlPlugin;
ctrl.Load();
parent.Controls.Add(ctrl);
ctrl.Parent = this;
}
}
public void UnloadControls(Control parent)
{
foreach(UserControl 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" <rcook@xxxxxxxxxxxx> wrote in message
news:%23vCq4e%23THHA.5108@xxxxxxxxxxxxxxxxxxxxxxx
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
.
- References:
- Need more efficient way to load/unload user controls on form
- From: Ronald S. Cook
- Re: Need more efficient way to load/unload user controls on form
- From: fitim skenderi
- Need more efficient way to load/unload user controls on form
- Prev by Date: Re: history of C#
- Next by Date: Re: Need more efficient way to load/unload user controls on form
- Previous by thread: Re: Need more efficient way to load/unload user controls on form
- Next by thread: Avoiding Exceptions
- Index(es):
Relevant Pages
|