How to Load multiple User control dynamically in C#
By FoxLearn 11/27/2024 11:31:52 AM 26.17K
To dynamically load multiple user controls into a panel in a C# Windows Forms application, you can follow these steps
How to load multiple User control dynamically in C#
Open your Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "LoadingUserControl" and then click OK button.
Create a Windows Form that contains the panel where you want to load the user controls.
Drag and drop Panel, Button controls from the Visual Studio toolbox onto your form designer, then design your form as shown below.
Create a Windows Form with a Panel control named panel
where you want to load the user controls.
Let's say you have 3 user controls named ucModule1, ucModule2
and ucModule3
.
How to create user control in C#?
Right-click on your project in the Solution Explorer, then select Add > New Item.
Choose User Control from the list of templates, then give it a name and click Add.
Once the user control is created, Visual Studio will open the Designer view for your user control.
In the Designer view, you can drag and drop controls (like buttons, labels, textboxes, etc.) from the Toolbox to design your user control.
Create the user controls that you want to load dynamically into the panel, then design the user interface and functionality of each user control according to your specific requirements.
For example:
ucModule1
Add code to handle ucModule1 as shown below.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace LoadingUserControl { public partial class ucModule1 : UserControl { // Using singleton pattern to create an instance to ucModule1 private static ucModule1 _instance; public static ucModule1 Instance { get { if (_instance == null) _instance = new ucModule1(); return _instance; } } public ucModule1() { InitializeComponent(); } } }
The ucModule1
class uses the Singleton pattern to ensure that only one instance of the ucModule1
user control is created.
The Instance
property checks if the _instance
is null, and if so, creates a new instance of the ucModule1
control. This ensures that every time Instance
is accessed, it returns the same object.
ucModule2
Add code to handle ucModule2 as shown below.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace LoadingUserControl { public partial class ucModule2 : UserControl { // Using singleton pattern to create an instance to ucModule2 private static ucModule2 _instance; public static ucModule2 Instance { get { if (_instance == null) _instance = new ucModule2(); return _instance; } } public ucModule2() { InitializeComponent(); } } }
ucModule3
Add code to handle ucModule3 as shown below.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace LoadingUserControl { public partial class ucModule3 : UserControl { // Using singleton pattern to create an instance to ucModule3 private static ucModule3 _instance; public static ucModule3 Instance { get { if (_instance == null) _instance = new ucModule3(); return _instance; } } public ucModule3() { InitializeComponent(); } } }
Use C# code to dynamically load the user controls into the panel. You can handle your main form as shown below.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace LoadingUserControl { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnModule1_Click(object sender, EventArgs e) { // Add module1 to panel control if (!panel.Controls.Contains(ucModule1.Instance)) { panel.Controls.Add(ucModule1.Instance); ucModule1.Instance.Dock = DockStyle.Fill; ucModule1.Instance.BringToFront(); } else ucModule1.Instance.BringToFront(); } private void btnModule2_Click(object sender, EventArgs e) { // Add module2 to panel control if (!panel.Controls.Contains(ucModule2.Instance)) { panel.Controls.Add(ucModule2.Instance); ucModule2.Instance.Dock = DockStyle.Fill; ucModule2.Instance.BringToFront(); } else ucModule2.Instance.BringToFront(); } private void btnModule3_Click(object sender, EventArgs e) { // Add module3 to panel control if (!panel.Controls.Contains(ucModule3.Instance)) { panel.Controls.Add(ucModule3.Instance); ucModule3.Instance.Dock = DockStyle.Fill; ucModule3.Instance.BringToFront(); } else ucModule3.Instance.BringToFront(); } } }
For each button click we will check if it exists in the Panel control, if not we will add it, else we can use the BringToFront
method to show it.
You can also create the LoadUserControl to help you dynamically load multiple user control to the Panel.
private void LoadUserControl(Type controlType) { UserControl userControl = (UserControl)Activator.CreateInstance(controlType); userControl.Dock = DockStyle.Fill; if (!panel.Controls.Contains(userControl)) { panel.Controls.Add(userControl); userControl.Instance.Dock = DockStyle.Fill; userControl.Instance.BringToFront(); } else userControl.Instance.BringToFront(); }
For example to load module1
private void btnModule1_Click(object sender, EventArgs e) { LoadUserControl(typeof(ucModule1)); }
In the form's code, you can use the Controls.Add
method to dynamically load multiple user controls into a panel in a C# Windows Forms application.
In this example, LoadUserControl
method loads a specified user control type into the panel. Three buttons (btnModule1
, btnModule2
and btnModule3
) are used to trigger the loading of ucModule1,
ucModule2
and ucModule3
, respectively.
VIDEO TUTORIAL