Windows Forms: How to load User control dynamically in C#

How to load user control dynamically in C#.

Step 1Click 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

loading user control c#Step 2: Design your form as below

user control c#

Create user controls as below

ucModule1

c# user control

Add code to handle ucModule1 as 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();
        }
    }
}

ucModule2

c# user control

Add code to handle ucModule2 as 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

c# user control

Add code to handle ucModule3 as 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();
        }
    }
}

Step 3: Add code to handle your form as 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();
        }
    }
}

VIDEO TUTORIALS