How to use Multiple Document Interface (MDI) in C#

By FoxLearn 7/16/2024 9:02:15 AM   6.94K
Creating an application contain multiple child form using Multiple Document Interface in C# Windows Forms.

Using Multiple Document Interface (MDI) in C# Windows Forms allows you to create applications that can contain multiple child windows within a single parent window.

MDI child forms are an essential element of Multiple-Document Interface Applications, as these forms are the center of user interaction.

How to use Multiple Document Interface in C#

Here's a basic guide on how to use MDI in C# Windows Forms

Open your Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "MdiForm" and then click OK

Open your main form in design view, then set its IsMdiContainer property to true. You can do this in the form's properties window in Visual Studio.

mdi form in c#

From the Visual Studio toolbox, drag and drop the MenuStrip control to the form, then add items to your menu

Next, create the forms that you want to be hosted within the MDI container. These forms will serve as child windows. Add new forms to your project for each child window you need.

For example: Form2, Form3, Form4, Form5

In each child form, set the MdiParent property to the main MDI container form. This can be done in the child form's constructor or before showing the child form.

Add code to handle your form as below

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MdiForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //Show MDI form
        private void ShowForm(Form frm)
        {
            frm.MdiParent = this;
            frm.Show();
            frm.Activate();
        }

        private void form1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm(Form2.Instance);
        }

        private void form2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowForm(Form3.Instance);
        }

        private void form3ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form4 frm = new Form4();
            frm.MdiParent = this;
            frm.Show();
        }

        private void form4ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form5 frm = new Form5();
            frm.MdiParent = this;
            frm.Show();
        }
    }
}

You can then create instances of your child forms and display them within the MDI container. Typically, this is done from an event handler or a menu item click event.

You can manage child forms just like any other form. They can be minimized, maximized, closed, etc. You can also access and manipulate child forms from the parent form.

// Close all child forms
foreach (Form form in this.MdiChildren)
{
    form.Close();
}

Child forms can have properties like any other form. You can set their title, icon, size, position, etc., as needed.

You can also customize the appearance and behavior of MDI forms using various properties and events. This includes setting MDI parent background, handling child form activation, etc.

By following these steps, you can create a C# Windows Forms application that utilizes the Multiple Document Interface (MDI) to host multiple child windows within a single parent window.

VIDEO TUTORIAL

Related