Example with MDI Froms:-



  • Create A New Windows Forms Application with the Name WAMDI
  • By Deafault WAMDI Contains Form1
  • Add Two New Forms to the Application with a the name Form2,FormMDI
  • Create a label on Form2

Set the Following Propertie(s) to the label1:-

Label1:-

Name          :         lblDisplay
Text            :         This is Form1

Create a label on Form2
Set the Following Propertie(s) to the label1:-

label1:-

Name          :         lblDisplay
Text            :         This is Form2

FormMDI:-

Name                    :         FormMDI
Text                      :         FormMDI
IsMdiContainer     :         True

Create one MenuStrip  Control on FormMDI & set the following properties to it
[menuStrip1 will only appear in Component tray]


menuStrip1:-

Name          :         menuStrip1
         
Set fallowing properties to ToolStripMenuItem  properties:


Open:-

Name          :         mnuOPen
Text            :         &Open

Form1:-

Name          :         smnuOForm1
Text            :         Form&1

From2:-

Name          :         smnuOForm2
Text            :         Form&2

Close:-

Name          :         mnuClose
Text            :         &Close

Form1:-

Name          :         smnuCForm1
Text            :         Form1

From2:-

Name          :         smnuCForm2
Text            :         Form2



Window:-

Name          :         mnuWindow
Text            :         &Window

TileHorizontally:-

Name          :          smnuTileHorizontally
Text            :         TileHorizontally

TileVertically:-

Name          :         smnuTileVertically
Text            :         TileVertically

Cascade:-

Name          :         smnuCascade
Text            :         Cascade





Design :-

Form1

Form2
FormMDI

  • Generate the Event Handlers for
  • smnuOForm1,smnuOForm2,smnuCForm1,smnuCForm2,smnuTileHorizontally,smnuTileVertically,smnuCascade Click Events& write the following code in Source[FormMenu.cs]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WAMDI
{
    public partial class FormMDI : Form
    {
        public static Form1 obj1 = new Form1();
        public static Form2 obj2 = new Form2();
        public FormMDI()
        {
            InitializeComponent();
        }

        private void smnuOForm1_Click(object sender, EventArgs e)
        {
            if (obj1 == null)
            {
                obj1 = new Form1();
            }
            obj1.MdiParent = this;
            obj1.Show();
        }

        private void smnuOForm2_Click(object sender, EventArgs e)
        {
            if (obj2 == null)
            {
                obj2 = new Form2();
            }
            obj2.MdiParent = this;
            obj2.Show();
        }

        private void smnuExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void smnuCForm1_Click(object sender, EventArgs e)
        {
            obj1.Hide();
            FormMDI.obj1 = null;
        }

        private void smnuCForm2_Click(object sender, EventArgs e)
        {
            obj2.Hide();
            FormMDI.obj2 = null;
        }

        private void smnuTileHorizontal_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void smnuTileVertical_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileVertical);
        }

        private void smnuCascade_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.Cascade);
        }
    }
}




Result :-

Run the Application & Check



No comments:

Post a Comment