Click here to Skip to main content
13,293,173 members (36,870 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


10 bookmarked
Posted 10 Jul 2014

Creating MDI Forms in WinForms

, 11 Jul 2014
Rate this:
Please Sign up or sign in to vote.
Creating MDI Forms in WinForms

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 WinForms

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:

The 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 Form2.

Form 2 has a button that calls Form3.

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.

Coding Logic

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();

          if(this.ActiveMdiChild!=null )

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.

The function 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))
            return i;

Similarly the forward button when pressed invokes the following method – btn_Next_Click().

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:

public MainForm()
          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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Australia Australia
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 4 Pin
mr. Duan14-Jul-14 17:25
professionalmr. Duan14-Jul-14 17:25 
QuestionHmmm... Pin
joso12-Jul-14 9:20
memberjoso12-Jul-14 9:20 
Suggestionfeedback Pin
Member 1093114311-Jul-14 23:59
memberMember 1093114311-Jul-14 23:59 
GeneralMy vote of 1 Pin
Ph.E11-Jul-14 10:38
memberPh.E11-Jul-14 10:38 
QuestionNo more midi forms Pin
emrea11-Jul-14 6:14
memberemrea11-Jul-14 6:14 
AnswerRe: No more midi forms Pin
Vitilevu11-Jul-14 16:56
memberVitilevu11-Jul-14 16:56 
QuestionFurther info on MDI Pin
Graham Lemon (UK)11-Jul-14 2:31
professionalGraham Lemon (UK)11-Jul-14 2:31 
GeneralMy vote of 3 Pin
CatchExAs10-Jul-14 14:02
professionalCatchExAs10-Jul-14 14:02 
SuggestionMDI not MIDI Pin
Brad Bruce10-Jul-14 12:12
memberBrad Bruce10-Jul-14 12:12 
GeneralRe: MDI not MIDI Pin
Sander Rossel10-Jul-14 12:22
professionalSander Rossel10-Jul-14 12:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 11 Jul 2014
Article Copyright 2014 by Vitilevu
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid