Home > DeveloperSection > Beginner > Master Page in C# Window forms- Part II

Master Page in C# Window forms- Part II


ASP.Net ASP.Net 
Ratings:
0 Comment(s)
 7269  View(s)
Rate this:

Master Page in C# Window forms- Part II

 

Introduction

All we have used Master Page in Asp.net application but in window application we do not have Master Pages. So we need to write custom code to achieve similar functionality.

In my last article we have learn how to achieve master page functionality in C# winforms

Here I am going to demo one more example where we can use Master Page in C# winforms.

Let we have list of pages (winforms) which need to be linked from every page in application.

So if our application contains 100 of winforms then it is tedious task to add those controls 100 times

So here we will use Master page concept to make this functionality.

Steps

We will create a Form which would be treated as Master form, We will add Toolstrip to this for and place various controls like Add Form, update Forms, Delete Forms etc. then we will use this form to all those forms where we need these controls on page.

  1. Add window application as per screen below

Master Page in C# Window forms- Part II

  1. Add a form and named as MastertoolStrip

Currently this for is blank

Master Page in C# Window forms- Part II

  1. Add TooStrip to this page

Master Page in C# Window forms- Part II

We added string FormId and overloaded Constructor MasterForm(string id)

  1. Add two forms named as child1.cs and child2.cs

Master Page in C# Window forms- Part II

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

  1. Code for Child1.cs

We added Child1 label on UI, just to know which UI have opened

If we see code of child1.cs, this form is automatically inherited by Form Class.

Master Page in C# Window forms- Part II

 

  1. We are going to use Concept of Inheritance in this Project
  2. Change Inherited Class from Form to MasterForm in child1.cs as per screen below

Master Page in C# Window forms- Part II

  1. Open Designer of Child1.cs

Master Page in C# Window forms- Part II

Understanding the code

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

 

  1. Add Following code to child page
  2. To capture Add and Update Event in child page just add following methods to child page

public void Add()

        {

            MessageBox.Show("Child 1 Add ");

        }

        public void UpdateData()

        {

            MessageBox.Show("Child 1 Update ");

 

        }

  1. Call base class constructor from child page

public partial class Child1 : MasterForm

    {

        public Child1() : base("Child1")  // call base class constructor and pass FormID as name

        {

            InitializeComponent();

        }

 

        private void Child1_Load(object sender, EventArgs e)

        {}

}

 

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

   public partial class MasterForm : Form

    {

        string FormID = string.Empty;  // we will get value of FORMID from Child page

        public MasterForm()

        {

            InitializeComponent();

        }

public MasterForm(string id)    // this constructor will be called from child page

        {

            this.FormID = id;

            InitializeComponent();

        }

 

        private void MasterForm_Load(object sender, EventArgs e)

        {

 

        }

   }

  1. Add button click event on MasterForm

private void btnAdd_Click(object sender, EventArgs e)

        {

 

           MessageBox.Show("Add of Master form called first");

           if (FormID == "Child1")

            {

                Child1 o = new Child1();

                o.Add();

            }

 

            if (FormID == "Child2")

            {

                Child2 o = new Child2();

                o.Add();

            }

        }

Understanding Code

·         As soon we run Child1 for Base class (MasterForm) constructor get called

·         We have Passed Child1 a string to base class constructor

·         As user click on button Add , which appears on Child page, behind the scence btnAdd_click of MasterForm will be called

·         We have if condition to check which Form is Running, Here in this case Child1 is running and we passed Child1 as string from Constructor

·         Inside If condition we are creating Object of Child1.cs to call Add/Update button.

  1. Running the Code

Master Page in C# Window forms- Part II

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

Now we clicked on Ok button of Alert box

Master Page in C# Window forms- Part II

 

Complete Code

MasterForm.cs

public partial class MasterForm : Form

    {

        string FormID = string.Empty;

        public MasterForm()

        {

            InitializeComponent();

        }

        public MasterForm(string id)

        {

            this.FormID = id;

            InitializeComponent();

        }

 

        private void MasterForm_Load(object sender, EventArgs e)

        {

 

        }

 

       private void btnAdd_Click(object sender, EventArgs e)

        {

 

           MessageBox.Show("Add of Master form called first");

           if (FormID == "Child1")

            {

                Child1 o = new Child1();

                o.Add();

            }

 

            if (FormID == "Child2")

            {

                Child2 o = new Child2();

                o.Add();

            }

        }

 

        private void btnUpdate_Click(object sender, EventArgs e)

        {

            MessageBox.Show("Update of Master form called first");

            if (FormID == "Child1")

            {

                Child1 o = new Child1();

                o.UpdateData();

            }

 

            if (FormID == "Child2")

            {

                Child2 o = new Child2();

                o.UpdateData();

            }

        }

      

    }

Child1.cs

public partial class Child1 : MasterForm

    {

        public Child1() : base("Child1")

        {

            InitializeComponent();

        }

 

        private void Child1_Load(object sender, EventArgs e)

        {

 

        }

        public void Add()

        {

            MessageBox.Show("Child 1 Add ");

        }

        public void UpdateData()

        {

            MessageBox.Show("Child 1 Update ");

 

        }

 

       

      

    }

Master form.cs


Don't want to miss updates? Please click the below button!

Follow MindStick