CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Winform Control Stacking

538 Views
Copy Code Show/Hide Line Numbers
private void button_Click(object sender, EventArgs e) 
{ 
    // pass in the containing panel 
    LoadControl<MyControls.MyControl>(panelContainer); 
} 
 
void LoadControl<T>(Panel panel) where T : Control, new() 
{ 
    T _Control = GetControl<T>(panel); 
    if (_Control == null) 
    { 
        _Control = new T(); 
        _Control.Dock = DockStyle.Fill; 
        panel.Controls.Add(_Control); 
    } 
    _Control.BringToFront(); 
} 
 
T GetControl<T>(Panel panel) where T : Control 
{ 
    Type _Type = typeof(T); 
    String _Name = _Type.ToString(); 
    if (!panel.Controls.ContainsKey(_Name)) 
        return null; 
    T _Control = panel.Controls[_Name] as T; 
    return _Control; 
} 
by Jerry Nixon
  March 05, 2010 @ 8:17am
Tags:
Description:
This example code assumes a few things.
1) that your user controls are singletons.
2) that your user control has an empty constructor.
3) that you have a base panel called panelContainer.

But it gets you a LONG way to the goal.

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate