C# Windows Forms MdiForm Examples

Let us look at a simple C# Windows Forms MdiForm example.

Form.MdiParent

This property Gets or sets the current multiple-document interface (MDI) parent form of this form.

[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.Form MdiParent { get; set; }

To create an MDI child form, assign the Form that will be the MDI parent form to the MdiParent property of the child form. You can use this property from an MDI child form to obtain global information that all child forms need or to invoke methods that perform actions to all child forms.

Form.IsMdiChild Property

Gets a value indicating whether the form is a multiple-document interface (MDI) child form.

[System.ComponentModel.Browsable(false)]
public bool IsMdiChild { get; }

At run time, MDI child forms are displayed inside the client area of an MDI parent form. An MDI child form can be maximized, minimized, and moved within the MDI parent form. To create an MDI child form, assign the Form that will be the MDI parent form to the MdiParent property of the child form. You can use the IsMdiContainer property to determine whether a form is an MDI parent form.

You can use the IsMdiChild property to determine whether a form returned by a method or property is an MDI child form or a standard form in your application such as a dialog box.

Form.IsMdiContainer Property

Gets or sets a value indicating whether the form is a container for multiple-document interface (MDI) child forms.

public bool IsMdiContainer { get; set; }

true if the form is a container for MDI child forms; otherwise, false. The default is false.

This property changes the display and behavior of the form to an MDI parent form. When this property is set to true, the form displays a sunken client area with a raised border. All MDI child forms assigned to the parent form are displayed within its client area.

When an MDI parent form is closed, the Closing events of all MDI child forms are raised before the MDI parent form's Closing event is raised. In addition, the Closed events of all MDI child forms are raised before the Closed event of the MDI parent form is raised.

Example 1: MdiParent - How to create and show child Forms

The following example demonstrates how to create child forms in an MDI application. The example code creates a form with unique text to identify the child form. The example uses the MdiParent property to specify that a form is a child form. This example requires that the code in the example is called from a form that has its IsMdiContainer property set to true and that the form has a private class level integer variable named childCount.

Step 1: Create Project

Start by creating a windows Form project.

Step 2: Write Code

Start by creating our method which will create and show these child forms:

private void CreateMyChildForm ()
{

Then Create a new form to represent the child form:

   Form child = new Form();

Then Increment the private child count:

   childCount++;

Set the text of the child form using the count of child forms:

   String formText = "Child " + childCount;
   child.Text = formText;

Then Make the new form a child form:

   child.MdiParent = this;

Finally Display the child form:

   child.Show();
}

Here is the full code:

private void CreateMyChildForm ()
{
   // Create a new form to represent the child form.
   Form child = new Form();
   // Increment the private child count.
   childCount++;
   // Set the text of the child form using the count of child forms.
   String formText = "Child " + childCount;
   child.Text = formText;

   // Make the new form a child form.
   child.MdiParent = this;
   // Display the child form.
   child.Show();
}

Example 2: IsMdiContainer - Change Color of MdiForm

The following example demonstrates using the IsMdiContainer property as well as changing the BackColor property of an MDI Form. To run this example, paste the following code in a new form.

Create a new form:

Form mdiChildForm = new Form();

When Form1 is Loaded:

private void Form1_Load(object sender, System.EventArgs e)
{

Set the IsMdiContainer property to true.

    IsMdiContainer = true;

Set the child form's MdiParent property to the current form:

    mdiChildForm.MdiParent = this;

Call the method that changes the background color:

    SetBackGroundColorOfMDIForm();
}

Here is how you loop through all MdiForms and set their background colors:

private void SetBackGroundColorOfMDIForm()
{
    foreach ( Control ctl in this.Controls )
    {
        if ((ctl) is MdiClient)

            // If the control is the correct type,
            // change the color.
        {
            ctl.BackColor = System.Drawing.Color.PaleGreen;
        }
    }
}

Here is the full code:

// Create a new form.
Form mdiChildForm = new Form();

private void Form1_Load(object sender, System.EventArgs e)
{

    // Set the IsMdiContainer property to true.
    IsMdiContainer = true;

    // Set the child form's MdiParent property to 
    // the current form.
    mdiChildForm.MdiParent = this;

    // Call the method that changes the background color.
    SetBackGroundColorOfMDIForm();
}

private void SetBackGroundColorOfMDIForm()
{
    foreach ( Control ctl in this.Controls )
    {
        if ((ctl) is MdiClient)

            // If the control is the correct type,
            // change the color.
        {
            ctl.BackColor = System.Drawing.Color.PaleGreen;
        }
    }
}

Example 3: MdiForm

This example will comprise the following files:

  • MdiForm.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

Write Code as follows:

*(a). MdiForm.cs

Create a file named MdiForm.cs

Here is the full code

using System;
using System.Windows.Forms;

namespace Examples {
  class MdiChildForm : Form {
    public MdiChildForm() {
      this.richTextBox.Parent = this;
      this.richTextBox.Dock = DockStyle.Fill;
    }

    private RichTextBox richTextBox = new RichTextBox();
  }

  class MdiParentForm : Form {
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new MdiParentForm());
    }

    public MdiParentForm() {
      this.mainMenu.MenuItems.Add("File");
      this.mainMenu.MenuItems[0].MenuItems.Add(new MenuItem("New", this.OnMenuFileNewClick, Shortcut.CtrlN));
      this.mainMenu.MenuItems[0].MenuItems.Add(new MenuItem("Close", this.OnMenuFileCloseClick, Shortcut.CtrlW));
      this.mainMenu.MenuItems[0].MenuItems.Add(new MenuItem("-"));
      this.mainMenu.MenuItems[0].MenuItems.Add(new MenuItem("Quit", this.OnMenuFileQuitClick, Environment.OSVersion.Platform >= PlatformID.Unix ? Shortcut.CtrlQ : Shortcut.AltF4));

      this.Text = "Mdi example";
      this.IsMdiContainer = true;
      this.Size = new System.Drawing.Size(800, 600);
      this.Menu = this.mainMenu;
    }

    void OnMenuFileNewClick(object sender, EventArgs e) {
      MdiChildForm child = new MdiChildForm();
      child.MdiParent = this;
      child.Text = string.Format("child {0}", ++this.childCounter);
      child.Show();
    }

    void OnMenuFileCloseClick(object sender, EventArgs e) {
      if (this.ActiveMdiChild != null)
        this.ActiveMdiChild.Close();
    }

    void OnMenuFileQuitClick(object sender, EventArgs e) {
      this.Close();
    }

    private MainMenu mainMenu = new MainMenu();
    private int childCounter = 0;
  }
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Related Posts