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

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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






.



Relevant Pages

  • Re: How to Design Delegate Architecture ?
    ... public void DoSomething(){ ... controls with no need of if..then or selection constructs. ... private static HandleSpecific _handler; ...
    (microsoft.public.dotnet.languages.csharp)
  • CompositeControl Design Time Error (Could not add List<> property to the collection)
    ... 'MenuItems' could not be initialized. ... Here's the code I'm using for the composite control: ... public void ClearMenuItems() ... // Create and build Header controls. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Recommendations for Outlook style interface
    ... applications like these for a little while. ... Create Form that will be used to display my controls in (usually by ... Create a Panel on that form (this will host the custom controls), ... public void ShowCustomUserControl_1 ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Recommendations for Outlook style interface
    ... applications like these for a little while. ... Create Form that will be used to display my controls in (usually by ... Create a Panel on that form (this will host the custom controls), ... public void ShowCustomUserControl_1 ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: Recommendations for Outlook style interface
    ... applications like these for a little while. ... Create Form that will be used to display my controls in (usually by ... Create a Panel on that form (this will host the custom controls), ... public void ShowCustomUserControl_1 ...
    (microsoft.public.dotnet.languages.csharp)