Need more efficient way to load/unload user controls on form

R

Ronald S. Cook

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
 
A

aaron.kempf

I would use VB.

VB is obviously more efficient than CSharp

I mean; CSharp has only been around for a couple of years now; it's
basically still in beta
 
F

fitim skenderi

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
 
R

Ronald S. Cook

Thanks, I'll try that. Any other options?


fitim skenderi said:
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
 
P

pfc_sadr

why is it not true?

C sharp is still in beta; I mean; it's only been around for a couple
of years

it's sort of like 'dont ever buy windows until sp1 comes out'

except csharp needs about FORTY ****ING YEARS TO CATCH UP TO VB
 
P

punjab_tom

how is it not true?

c# was invented for no reason; because MS didn't understand or
appreciate the popularity of VB

that is the only reason


and now .NET is a 1/4 replacement for Vb6-- I can't use .NET in
ActiveX scripts; I can't use .NET in office or in VBS files




and I can't use vb.net or vbs on the clientside of a browser


IS THIS PROGRESS??
 

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