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.

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 ");
  8. }

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. {}
  9. }

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. MessageBox.Show("Add of Master form called first");
  4. if (FormID == "Child1")
  5. {
  6. Child1 o = new Child1();
  7. o.Add();
  8. }
  9. if (FormID == "Child2")
  10. {
  11. Child2 o = new Child2();
  12. o.Add();
  13. }
  14. }

The following explains the code.

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. private void MasterForm_Load(object sender, EventArgs e)
  14. {
  15. }
  16. private void btnAdd_Click(object sender, EventArgs e)
  17. {
  18. MessageBox.Show("Add of Master form called first");
  19. if (FormID == "Child1")
  20. {
  21. Child1 o = new Child1();
  22. o.Add();
  23. }
  24. if (FormID == "Child2")
  25. {
  26. Child2 o = new Child2();
  27. o.Add();
  28. }
  29. }
  30. private void btnUpdate_Click(object sender, EventArgs e)
  31. {
  32. MessageBox.Show("Update of Master form called first");
  33. if (FormID == "Child1")
  34. {
  35. Child1 o = new Child1();
  36. o.UpdateData();
  37. }
  38. if (FormID == "Child2")
  39. {
  40. Child2 o = new Child2();
  41. o.UpdateData();
  42. }
  43. }
  44. }

Child1.cs

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