How to Display another UserControl by Clicking on button in One UserControl

User Controls in C#

In this tutorial we’ll talk about how to Display UserContol from other UserControl in c#. UserControl inherits all the standard positioning and mnemonic-handling code that is necessary in a user control. The UserControl gives you the ability to create controls that can be used in multiple places within an application. ( Display UserContol )

But Now a days we UserControls can be used as partial pages of our applications.

Tools Required:

  1. Visual Studio 2010 or Later version.
  2. Pichon Desktop app for icons (Optional). Download it from Here

Steps to Follow:

For demonstration purpose, just add two User Controls in your project. Name the first controls as ‘UCHome’ which will be our main user control.We will place a button control on this User Control by clicking which the other User Control will be displayed.

Name the other User Control as ‘UCNext’ (You can name it what ever you want). Add a Panel control from tool box which will work as a container for our User Controls and name it as ‘PnlContainer’.

We will also place a button on Main Form to come back to our main screen. name it ‘btnBack’

Go to code view of your Main Form (Form1) and add the following code.

static Form1 _obj;

        public static Form1 Instance
        {
            get
            {
                if (_obj == null)
                {
                    _obj = new Form1();
                }
                return _obj;
            }
        }

        public Panel PnlContainer
        {
            get { return panelContainer; }
            set { panelContainer = value; }
        }

        public Button BackButton
        {
            get { return btnBack; }
            set { btnBack = value; }
        }

After that, just go to the Load Event of your Main Form and add the Following Code:

btnBack.Visible = false;
_obj = this;

        UCHome uc = new UCHome();
        uc.Dock = DockStyle.Fill;
        panelContainer.Controls.Add(uc);
//This will display UCHome when form loads

Go to the OnClick event of Back button and add the following code:

panelContainer.Controls[“UCHome”].BringToFront();
btnBack.Visible = false;
//This will display the UCHome back

Go to the designer view of UCHome and add a button control which will be used to navigate to the other User Control. In Click event of this button, add the following Code:

 if(!Form1.Instance.PnlContainer.Controls.ContainsKey("UCNext"))
            {
                UCNext un = new UCNext();
                un.Dock = DockStyle.Fill;
                Form1.Instance.PnlContainer.Controls.Add(un);
            }
  Form1.Instance.PnlContainer.Controls["UCNext"].BringToFront();
  Form1.Instance.BackButton.Visible = true;

Video Tutorial:

Full Video Tutorial

Source Code:

14 comments

  1. I savour, lead to I found just what I used to be having a look for. You’ve ended my 4 day long hunt! God Bless you man. Have a nice day. Bye|

  2. Awesome! Its genuinely remarkable post, I have got much clear idea on the topic of from this article.|

  3. A person essentially help to make significantly articles I would state. This is the first time I frequented your web page and so far? I surprised with the research you made to create this particular publish amazing. Magnificent process!

  4. Exceptional post but I was wondering if you could write a litte more on this topic? I’d be very thankful if you could elaborate a little bit further. Bless you!

  5. Thanks for the good writeup. It actually used to be a enjoyment account it. Look complex to more added agreeable from you! However, how can we communicate?|

  6. I am really impressed with your writing skills as well as with the layout on your weblog. Is this a paid theme or did you customize it yourself? Anyway keep up the excellent quality writing, it’s rare to see a great blog like this one today.|

  7. My family all the time say that I am killing my time here at web, however I know I am getting know-how all the time by reading thes good articles or reviews.|

  8. Everything is very open with a precise clarification of the challenges. It was definitely informative. Your site is very helpful. Thanks for sharing!

  9. I’m not that much of a online reader to be honest but your blogs really nice, keep it up! I’ll go ahead and bookmark your website to come back down the road. All the best

Comments are closed.