Master Pages in C# Windows Forms Applications

This article shows how to create Master Pages in C# Windows Forms Applications.

Introduction

We have all used Master Pages in ASP.NET applications but in a Windows application we do not have Master Pages. So we need to write custom code to get similar functionality.

In this sample application we will create a common control that we need for all the forms in the application.

These controls could be Add, Update, Delete, Refresh and Close.

Let's create a simple form first that contains all the these controls.

Use the following procedure to create a sample.

1. Add a Windows Forms application as in the screen below:

Window form

2. Add a form and name it Masterform. Add two buttons, Add and Update.

Add two buttons

3. Code for MasterForm:

Code for masterform
We added the string FormId and overloaded the constructor MasterForm(string id).

4. Add two forms named child1 and child2 as in the following:

Add two forms

Here our objective is to make the behavior of the child page with child1.cs and child2.cs.

5. Code for Child1

We added a Child1 label on the UI, just to know which UI has been opened.

If we see the code of child1 then this form automatically inherits the Form Class.

Form Class

6. We will use inheritance in this project.

7. Change the inherited class from Form to MasterForm in child1.cs as in the screen below:

Class

8. Open the Designer for Child1.cs.

Open Designer

The following explains the code.

  • As soon as we open Child1 in the designer page we can see Add and Update buttons on the Child1 page.
  • These Add and Update buttons are only due to inheritance as we did in the screen below.
  • Here the Masterforms class acts as a master form and Child1 acts as a child form, only due to inheritance.
  • Inheritance Code: public partial class Child1 : MasterForm
  • The Child1 class inherits from MasterForm
  • Since these buttons are designed on the Master form and we are looking at these buttons in the Child1 form, we can see a small lock over it.
  • We cannot move these buttons from the child page

9. Add the following code to the child page:

10. To capture an Add and Update Event in the child page we just add the following methods to the child page.

  1. public void Add()  
  2. {  
  3.    MessageBox.Show("Child 1 Add ");  
  4. }  
  5. public void UpdateData()  
  6. {  
  7.    MessageBox.Show("Child 1 Update ");  

11. Call the base class constructor from the child page:

  1. public partial class Child1 : MasterForm  
  2. {  
  3.     public Child1() : base("Child1")  // call base class constructor and pass FormID as name  
  4.     {  
  5.       InitializeComponent();  
  6.     }   
  7.     private void Child1_Load(object sender, EventArgs e)  
  8.     {}  

12. From the code above the MasterForm(string Id) constructor will be called.

  1. public partial class MasterForm : Form  
  2. {  
  3.     string FormID = string.Empty;  // we will get value of FORMID from Child page     
  4.     public MasterForm()  
  5.     {  
  6.        InitializeComponent();  
  7.     }  
  8.    public MasterForm(string id)    // this constructor will be called from child page  
  9.    {  
  10.         this.FormID = id;  
  11.         InitializeComponent();  
  12.    }   
  13.    private void MasterForm_Load(object sender, EventArgs e)  
  14.    {  
  15.   
  16.    }  

13. Add a button click event on the MasterForm

  1. private void btnAdd_Click(object sender, EventArgs e)  
  2. {  
  3.   
  4.     MessageBox.Show("Add of Master form called first");  
  5.     if (FormID == "Child1")  
  6.     {  
  7.         Child1 o = new Child1();  
  8.         o.Add();  
  9.     }  
  10.   
  11.     if (FormID == "Child2")  
  12.     {  
  13.         Child2 o = new Child2();  
  14.         o.Add();  
  15.     }  

The following explains the code.

  • As soon as we run Child1 for the Base class the MasterForm constructor is called
  • We have passed Child1 as a string to the base class constructor
  • As the user clicks on the button Add that appears on the Child page, behind the scence btnAdd_click of MasterForm will be called
  • We have an if condition to check which form is running. Here in this case Child1 is running and we passed Child1 as a string from the constructor
  • Inside the If condition we are creating an object of Child1 to call an Add or Update button.

14. Running the Code.

Running the Code

We will get the screen above if we click on the Add button.

Now when we click on the Ok button of the Alert box:

Alert box

Complete Code

MasterForm.cs

  1. public partial class MasterForm : Form  
  2. {  
  3.         string FormID = string.Empty;  
  4.         public MasterForm()  
  5.         {  
  6.             InitializeComponent();  
  7.         }  
  8.         public MasterForm(string id)  
  9.         {  
  10.             this.FormID = id;  
  11.             InitializeComponent();  
  12.         }  
  13.   
  14.         private void MasterForm_Load(object sender, EventArgs e)  
  15.         {  
  16.   
  17.         }  
  18.   
  19.        private void btnAdd_Click(object sender, EventArgs e)  
  20.         {  
  21.   
  22.            MessageBox.Show("Add of Master form called first");  
  23.            if (FormID == "Child1")  
  24.             {  
  25.                 Child1 o = new Child1();  
  26.                 o.Add();  
  27.             }  
  28.   
  29.             if (FormID == "Child2")  
  30.             {  
  31.                 Child2 o = new Child2();  
  32.                 o.Add();  
  33.             }  
  34.         }  
  35.   
  36.         private void btnUpdate_Click(object sender, EventArgs e)  
  37.         {  
  38.             MessageBox.Show("Update of Master form called first");  
  39.             if (FormID == "Child1")  
  40.             {  
  41.                 Child1 o = new Child1();  
  42.                 o.UpdateData();  
  43.             }  
  44.   
  45.             if (FormID == "Child2")  
  46.             {  
  47.                 Child2 o = new Child2();  
  48.                 o.UpdateData();  
  49.             }  
  50.      }          

Child1.cs

  1. public partial class Child1 : MasterForm  
  2. {  
  3.     public Child1() : base("Child1")  
  4.     {  
  5.         InitializeComponent();  
  6.     }  
  7.   
  8.     private void Child1_Load(object sender, EventArgs e)  
  9.     {  
  10.   
  11.     }  
  12.     public void Add()  
  13.     {  
  14.        MessageBox.Show("Child 1 Add ");  
  15.     }  
  16.     public void UpdateData()  
  17.     {  
  18.         MessageBox.Show("Child 1 Update ");   
  19.     }