Language: C#
Winform Control Stacking
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; }
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.
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.
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

