Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

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 )
            {
                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:

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

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

Share

About the Author

Vitilevu
Software Developer
Australia Australia
No Biography provided

Comments and Discussions

 
GeneralMy vote of 4 Pinprofessionalmr. Duan14-Jul-14 16:25 
QuestionHmmm... Pinmemberjoso12-Jul-14 8:20 
Suggestionfeedback PinmemberMember 1093114311-Jul-14 22:59 
GeneralMy vote of 1 PinmemberPh.E11-Jul-14 9:38 
QuestionNo more midi forms Pinmemberemrea11-Jul-14 5:14 
AnswerRe: No more midi forms PinmemberVitilevu11-Jul-14 15:56 
QuestionFurther info on MDI PinprofessionalGraham Lemon (UK)11-Jul-14 1:31 
GeneralMy vote of 3 PinprofessionalCatchExAs10-Jul-14 13:02 
SuggestionMDI not MIDI PinmemberBrad Bruce10-Jul-14 11:12 
GeneralRe: MDI not MIDI PinprofessionalSander Rossel10-Jul-14 11:22 

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

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

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