Overview & Objective
In this discussion, I will show how to create a WinForms application that makes use of MDI child forms. The key purpose is to demonstrate a basic usage of MDI Child forms in a simple Winforms application that contains 3 child forms. The forms application will feature the ability to navigate from one form to another via previous and next buttons located on the panel of the main form.
In Visual Studio 2012 (2010), you can create a WinForms application by selecting File->New Project-> and select ‘Windows Forms Application’ as follows:
Right click on the project and select ‘Add’ followed by ‘Windows Form’.
Then, name the form as ‘
MainForm’. This will create the form ‘MainForm.cs’ as follows in the solution explorer:
Double click on ‘MainForm.cs’ in the solution explorer and create the main form as follows:
The menu strip is colored ‘Tan’ with a single Menu option ‘Control’.
Its property is as follows:
DropDownItems will consist of 3 forms. These will need to be created as shown later.
The next layer of strip on the ‘
MainForm’ form is the
Panel containing the following properties:
The panel has two buttons defined as follows:
Btn_Previous to get previous form and
btn_Next to get next form.
Creating the 3 Child Forms
Right click on the project in the solution explorer and create the following forms as below:
Form 1 has a button that calls
Form 2 has a button that calls
Form 3 is a plain form as above.
Running the Application
When you execute the program via F5, the following window pops up.
Click on Control menu and select ‘Go to Form 1’. This will take you to the following screen.
Click on the button ‘Get Form 2’ to go to the following screen:
Clicking on the ‘Go to Form 3’ button will take you to the final form as shown below.
When you click on the previous button indicated by the button, it will take you back to
Form2. In the
Form2 window, clicking on the button will take you back to the
Form3 button. From
Form3, clicking the back button twice will take you back to
Form1 and so forth. This allows for full navigation between the forms be it to the right or left.
Behind the scenes, the following code is required to make it happen.
When you click on the back button, the following code is invoked.
private void btn_Previous_Click(object sender, EventArgs e)
var curPos = GetCurrentPosition();
The above method located in MainForm.cs calls the ‘
GetCurrentPostion()’ function to get the current position of the calling form. Provided the current calling form is not
null, it will hide itself and reveal the form prior to it. The List variable
listOfForms will contain the list of forms that are active.
GetCurrentPosition() returns the current position of the calling MDI child form as follows:
private int GetCurrentPosition()
int i = 0;
if (this.ActiveMdiChild != null)
foreach (var form in listOfForms)
if (i == Convert.ToInt32( this.ActiveMdiChild.Tag))
Similarly the forward button when pressed invokes the following method –
private void btn_Next_Click(object sender, EventArgs e)
int curPos = GetCurrentPosition();
if (this.ActiveMdiChild != null)
if (listOfForms.Count > 0 && curPos+1 < listOfForms.Count)
listOfForms[curPos + 1].Show();
if (listOfForms.Count > 0 && listOfForms.Count != curPos)
It will call the
GetCurrentPosition() function to get the current position of the calling form and from there determine where to go, first ensuring the form is not
null before proceeding.
The List variable
listOfForms is declared as follows:
public static List<Form> listOfForms;
It is used to contain the list of Forms objects and accessible via the order in which it was placed in the list. It is initialised in the MainForms.cs constructor as follows:
listOfForms = new List<Form>();
To close up, the key point of this exercise is to have a List of forms objects in order to enable navigation through the list in either direction, depending on what button is pressed.