Page 1 of 1

Using MDI in C# How to create a MDI application using C# and WinForms.

#1 PixelCard  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 47
  • View blog
  • Posts: 139
  • Joined: 20-June 08

Posted 15 July 2008 - 06:19 AM

In this tutorial I will explain what is MDI and how to use it in C#.

MDI (Multiple Document Interface) is a type of GUI (Graphical User Interface), which presents a single parent (container) window for other windows in a specific application. The opposites of MDI are SDI (Single Document Interface) and TDI (Tabbed Document Interface).

The main advantages of MDI are:
  • Child windows are easily managed from a single parent (container) form.
  • A single menu and toolbar can be shared for other windows.
  • The possibility to work with multiple documents in one window of the same application.
  • Closing the parent (container) window, the user closes other child windows.

Special Tutorial Requirements:
  • C# IDE (Visual Studio 2008 used in this tutorial)
  • .NET Framework 1.0

So, let's start creating an MDI application.

1. Create a standard C# Windows Forms Application:

Posted Image

2. First of all I have to create a container form. The first form that was created by default can become a container just by changing the IsMdiContainer property to True:

Posted Image

Now, the form should look like this:

Posted Image

3. Now, I will add a new form to the project. It will be a child form. To add a new form, use the Add New Item button on the IDE toolbar (if you use Visual Studio). Select the Add Windows Form... option from the dropdown menu:

Posted Image

4. Now, when a new form is created, I need to call it from the parent form, so I will create a menu on the first form, using the MenuStrip control:

Posted Image

5. To the newly created menu I will add a root element (I will call it "Container") and a child element (called "New Child..."):

Posted Image

6. Now, to open a new child in the parent form, add this code to the "New Child..." menu item:

// Declare the child form as a new one.
Form2 childForm = new Form2();
// Set the main form as a parent form.
childForm.MdiParent = this;
// Show the child form.
childForm.Show();



Now, if you compile the application and click several times on the "New Child..." menu item, you will get new child forms inside the parent form:

Posted Image

7. Now, I will add some additional menu items, that will have the functionality needed to arrange the child forms inside the parent form.

Basically, there are four types of arrangement:
  • Cascade
  • Tile Horizontal
  • Tile Vertical
  • Arrange Icons

So, add the new menu items (as in the image below):

Posted Image

Here goes the code for all the types of arrangements (add this code to the corresponding menu items):

Cascade

// Set the layout to cascade.
this.LayoutMdi(MdiLayout.Cascade);



Screenshot:

Posted Image

Tile Horizontal

// Set the layout to tile horizontal.
this.LayoutMdi(MdiLayout.TileHorizontal);



Screenshot:

Posted Image

Tile Vertical

// Set the layout to tile vertical.
this.LayoutMdi(MdiLayout.TileVertical);



Screenshot:

Posted Image

Arrange Icons

// Set the layout to arrange icons.
this.LayoutMdi(MdiLayout.ArrangeIcons);



Screenshot:

Posted Image

Note: the ArrangeIcons layout is available only for minimized child forms.

8. Now, if the user opens many child forms, it becomes harder to navigate between them, so I will set the MdiWindowListItem of the menu strip to windowToolStripMenuItem, so all opened child windows will be listed in the Window menu:

Posted Image

As the application starts and child windows are open, these are listed in the Window menu:

Posted Image

Now I can easily switch between all opened child windows.

This tutorial shows only the basics of creating a MDI application, but it also shows some fundamental MDI ideas and implementations.

Attached File(s)



Is This A Good Question/Topic? 2
  • +

Replies To: Using MDI in C#

#2 Guest_Bravo*


Reputation:

Posted 24 April 2010 - 09:30 AM

View PostPixelCard, on 15 July 2008 - 05:19 AM, said:

In this tutorial I will explain what is MDI and how to use it in C#.

MDI (Multiple Document Interface) is a type of GUI (Graphical User Interface), which presents a single parent (container) window for other windows in a specific application. The opposites of MDI are SDI (Single Document Interface) and TDI (Tabbed Document Interface).

The main advantages of MDI are:
  • Child windows are easily managed from a single parent (container) form.
  • A single menu and toolbar can be shared for other windows.
  • The possibility to work with multiple documents in one window of the same application.
  • Closing the parent (container) window, the user closes other child windows.

Special Tutorial Requirements:
  • C# IDE (Visual Studio 2008 used in this tutorial)
  • .NET Framework 1.0

So, let's start creating an MDI application.

1. Create a standard C# Windows Forms Application:

Posted Image

2. First of all I have to create a container form. The first form that was created by default can become a container just by changing the IsMdiContainer property to True:

Posted Image

Now, the form should look like this:

Posted Image

3. Now, I will add a new form to the project. It will be a child form. To add a new form, use the Add New Item button on the IDE toolbar (if you use Visual Studio). Select the Add Windows Form... option from the dropdown menu:

Posted Image

4. Now, when a new form is created, I need to call it from the parent form, so I will create a menu on the first form, using the MenuStrip control:

Posted Image

5. To the newly created menu I will add a root element (I will call it "Container") and a child element (called "New Child..."):

Posted Image

6. Now, to open a new child in the parent form, add this code to the "New Child..." menu item:

// Declare the child form as a new one.
Form2 childForm = new Form2();
// Set the main form as a parent form.
childForm.MdiParent = this;
// Show the child form.
childForm.Show();



Now, if you compile the application and click several times on the "New Child..." menu item, you will get new child forms inside the parent form:

Posted Image

7. Now, I will add some additional menu items, that will have the functionality needed to arrange the child forms inside the parent form.

Basically, there are four types of arrangement:
  • Cascade
  • Tile Horizontal
  • Tile Vertical
  • Arrange Icons

So, add the new menu items (as in the image below):

Posted Image

Here goes the code for all the types of arrangements (add this code to the corresponding menu items):

Cascade

// Set the layout to cascade.
this.LayoutMdi(MdiLayout.Cascade);



Screenshot:

Posted Image

Tile Horizontal

// Set the layout to tile horizontal.
this.LayoutMdi(MdiLayout.TileHorizontal);



Screenshot:

Posted Image

Tile Vertical

// Set the layout to tile vertical.
this.LayoutMdi(MdiLayout.TileVertical);



Screenshot:

Posted Image

Arrange Icons

// Set the layout to arrange icons.
this.LayoutMdi(MdiLayout.ArrangeIcons);



Screenshot:

Posted Image

Note: the ArrangeIcons layout is available only for minimized child forms.

8. Now, if the user opens many child forms, it becomes harder to navigate between them, so I will set the MdiWindowListItem of the menu strip to windowToolStripMenuItem, so all opened child windows will be listed in the Window menu:

Posted Image

As the application starts and child windows are open, these are listed in the Window menu:

Posted Image

Now I can easily switch between all opened child windows.

This tutorial shows only the basics of creating a MDI application, but it also shows some fundamental MDI ideas and implementations.

Was This Post Helpful? 1

#3 Guest_Bravo*


Reputation:

Posted 24 April 2010 - 09:33 AM

Hi, how do you save the Childform files?
When I open a new Childform with a multiLine Textbox how do you save that file from the parent form SavemenuItems?
Was This Post Helpful? 0

#4 Guest_Ash*


Reputation:

Posted 03 November 2010 - 10:51 PM

View PostBravo, on 24 April 2010 - 08:33 AM, said:

Hi, how do you save the Childform files?
When I open a new Childform with a multiLine Textbox how do you save that file from the parent form SavemenuItems?


The easiest way i know how is to sent the information across in a new class.
So using the GET and SET features will allow you to move the info back to the parent form.
There is another way but noy sure how it will work, but in VS2008 C# there is a settings property,
i've been told you can use this feature to store dll files that way it will read the dll file everytime
the program is opened.

Hope it helps a little,
Im a jr. programmer, just started with all of this.
Was This Post Helpful? 0

#5 poptcat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-July 11

Posted 23 July 2011 - 06:04 AM

Hi! I tried to improve this sample, adding children to child windows:
http://blogcsharp.co.../basic-mdi-app/
Was This Post Helpful? 0
  • +
  • -

#6 gerrygohil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-February 13

Posted 06 February 2013 - 04:01 AM

View PostBravo, on 24 April 2010 - 09:30 AM, said:

View PostPixelCard, on 15 July 2008 - 05:19 AM, said:

In this tutorial I will explain what is MDI and how to use it in C#.

MDI (Multiple Document Interface) is a type of GUI (Graphical User Interface), which presents a single parent (container) window for other windows in a specific application. The opposites of MDI are SDI (Single Document Interface) and TDI (Tabbed Document Interface).

The main advantages of MDI are:
  • Child windows are easily managed from a single parent (container) form.
  • A single menu and toolbar can be shared for other windows.
  • The possibility to work with multiple documents in one window of the same application.
  • Closing the parent (container) window, the user closes other child windows.

Special Tutorial Requirements:
  • C# IDE (Visual Studio 2008 used in this tutorial)
  • .NET Framework 1.0

So, let's start creating an MDI application.

1. Create a standard C# Windows Forms Application:

Posted Image

2. First of all I have to create a container form. The first form that was created by default can become a container just by changing the IsMdiContainer property to True:

Posted Image

Now, the form should look like this:

Posted Image

3. Now, I will add a new form to the project. It will be a child form. To add a new form, use the Add New Item button on the IDE toolbar (if you use Visual Studio). Select the Add Windows Form... option from the dropdown menu:

Posted Image

4. Now, when a new form is created, I need to call it from the parent form, so I will create a menu on the first form, using the MenuStrip control:

Posted Image

5. To the newly created menu I will add a root element (I will call it "Container") and a child element (called "New Child..."):

Posted Image

6. Now, to open a new child in the parent form, add this code to the "New Child..." menu item:

// Declare the child form as a new one.
Form2 childForm = new Form2();
// Set the main form as a parent form.
childForm.MdiParent = this;
// Show the child form.
childForm.Show();



Now, if you compile the application and click several times on the "New Child..." menu item, you will get new child forms inside the parent form:

Posted Image

7. Now, I will add some additional menu items, that will have the functionality needed to arrange the child forms inside the parent form.

Basically, there are four types of arrangement:
  • Cascade
  • Tile Horizontal
  • Tile Vertical
  • Arrange Icons

So, add the new menu items (as in the image below):

Posted Image

Here goes the code for all the types of arrangements (add this code to the corresponding menu items):

Cascade

// Set the layout to cascade.
this.LayoutMdi(MdiLayout.Cascade);



Screenshot:

Posted Image

Tile Horizontal

// Set the layout to tile horizontal.
this.LayoutMdi(MdiLayout.TileHorizontal);



Screenshot:

Posted Image

Tile Vertical

// Set the layout to tile vertical.
this.LayoutMdi(MdiLayout.TileVertical);



Screenshot:

Posted Image

Arrange Icons

// Set the layout to arrange icons.
this.LayoutMdi(MdiLayout.ArrangeIcons);



Screenshot:

Posted Image

Note: the ArrangeIcons layout is available only for minimized child forms.

8. Now, if the user opens many child forms, it becomes harder to navigate between them, so I will set the MdiWindowListItem of the menu strip to windowToolStripMenuItem, so all opened child windows will be listed in the Window menu:

Posted Image

As the application starts and child windows are open, these are listed in the Window menu:

Posted Image

Now I can easily switch between all opened child windows.

This tutorial shows only the basics of creating a MDI application, but it also shows some fundamental MDI ideas and implementations.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1