Click here to Skip to main content
15,886,100 members
Articles / Programming Languages / C#

Creating MDI Forms in WinForms

Rate me:
Please Sign up or sign in to vote.
3.57/5 (12 votes)
11 Jul 2014CPOL3 min read 29.6K   19   11   10
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:

Image 1

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:

Image 2

Double click on ‘MainForm.cs’ in the solution explorer and create the main form as follows:

Image 3

The menu strip is colored ‘Tan’ with a single Menu option ‘Control’.

Its property is as follows:

Image 4

The DropDownItems will consist of 3 forms. These will need to be created as shown later.

Image 5

The next layer of strip on the ‘MainForm’ form is the Panel containing the following properties:

Image 6

The panel has two buttons defined as follows:

Image 7

Btn_Previous to get previous form and btn_Next to get next form.

Image 8Image 9

Creating the 3 Child Forms

Right click on the project in the solution explorer and create the following forms as below:

Image 10

Form 1 has a button that calls Form2.

Image 11

Form 2 has a button that calls Form3.

Image 12

Form 3 is a plain form as above.

Running the Application

When you execute the program via F5, the following window pops up.

Image 13

Click on Control menu and select ‘Go to Form 1’. This will take you to the following screen.

Image 14

Click on the button ‘Get Form 2’ to go to the following screen:

Image 15

Clicking on the ‘Go to Form 3’ button will take you to the final form as shown below.

Image 16

When you click on the previous button indicated by the Image 17 button, it will take you back to Form2. In the Form2 window, clicking on the Image 18 button will take you back to the Form3 button. From Form3, clicking the Image 19 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.

C#
private void btn_Previous_Click(object sender, EventArgs e)
      {
          var curPos = GetCurrentPosition();

          if(this.ActiveMdiChild!=null )
          {
              this.ActiveMdiChild.Hide();
          }
      }

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:

C#
private int GetCurrentPosition()
        {            
            int i = 0;
            if (this.ActiveMdiChild != null)
            {                
                foreach (var form in listOfForms)
                {
                    if (i == Convert.ToInt32( this.ActiveMdiChild.Tag))
                    {
                        break;
                    }
                    i++;
                }
            }
            return i;
        }

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

C#
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();
                }                
            }
            else
            {
                if (listOfForms.Count > 0 && listOfForms.Count != curPos)
                {
                    listOfForms[0].Show();
                }
            }
        }

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:

C#
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:

C#
public MainForm()
      {
          InitializeComponent();
          listOfForms = new List<Form>();

      }

Conclusion

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.

License

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


Written By
Software Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
mr. Duan14-Jul-14 16:25
professionalmr. Duan14-Jul-14 16:25 
QuestionHmmm... Pin
joso12-Jul-14 8:20
joso12-Jul-14 8:20 
Suggestionfeedback Pin
Pri_the_ITan11-Jul-14 22:59
Pri_the_ITan11-Jul-14 22:59 
GeneralMy vote of 1 Pin
User 682188111-Jul-14 9:38
User 682188111-Jul-14 9:38 
QuestionNo more midi forms Pin
emrea11-Jul-14 5:14
emrea11-Jul-14 5:14 
AnswerRe: No more midi forms Pin
Vitilevu11-Jul-14 15:56
Vitilevu11-Jul-14 15:56 
QuestionFurther info on MDI Pin
Graham Lemon (UK)11-Jul-14 1:31
professionalGraham Lemon (UK)11-Jul-14 1:31 
GeneralMy vote of 3 Pin
CatchExAs10-Jul-14 13:02
professionalCatchExAs10-Jul-14 13:02 
SuggestionMDI not MIDI PinPopular
Brad Bruce10-Jul-14 11:12
Brad Bruce10-Jul-14 11:12 
GeneralRe: MDI not MIDI Pin
Sander Rossel10-Jul-14 11:22
professionalSander Rossel10-Jul-14 11: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.