MDI Relatives : MDI « GUI Windows Form « C# / C Sharp






MDI Relatives

MDI Relatives
/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace MDIRelatives
{
  /// <summary>
  /// Summary description for MDIRelatives.
  /// </summary>
  public class MDIRelatives : System.Windows.Forms.Form
  {
    internal System.Windows.Forms.MainMenu MainMenu1;
    internal System.Windows.Forms.MenuItem MenuItem1;
    internal System.Windows.Forms.MenuItem mnuCascade;
    internal System.Windows.Forms.MenuItem mnuTileV;
    internal System.Windows.Forms.MenuItem mnuTileH;
    internal System.Windows.Forms.MenuItem mnuMinimizeAll;
    internal System.Windows.Forms.ImageList imgButtons;
    internal System.Windows.Forms.ToolBar ToolBar1;
    internal System.Windows.Forms.ToolBarButton cmdNew;
    internal System.Windows.Forms.ToolBarButton cmdClose;
    private System.ComponentModel.IContainer components;

    public MDIRelatives()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if(components != null)
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.components = new System.ComponentModel.Container();
      System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MDIRelatives));
      this.MainMenu1 = new System.Windows.Forms.MainMenu();
      this.MenuItem1 = new System.Windows.Forms.MenuItem();
      this.mnuCascade = new System.Windows.Forms.MenuItem();
      this.mnuTileV = new System.Windows.Forms.MenuItem();
      this.mnuTileH = new System.Windows.Forms.MenuItem();
      this.mnuMinimizeAll = new System.Windows.Forms.MenuItem();
      this.imgButtons = new System.Windows.Forms.ImageList(this.components);
      this.ToolBar1 = new System.Windows.Forms.ToolBar();
      this.cmdNew = new System.Windows.Forms.ToolBarButton();
      this.cmdClose = new System.Windows.Forms.ToolBarButton();
      this.SuspendLayout();
      // 
      // MainMenu1
      // 
      this.MainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                            this.MenuItem1});
      // 
      // MenuItem1
      // 
      this.MenuItem1.Index = 0;
      this.MenuItem1.MdiList = true;
      this.MenuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                            this.mnuCascade,
                                            this.mnuTileV,
                                            this.mnuTileH,
                                            this.mnuMinimizeAll});
      this.MenuItem1.Text = "Window";
      // 
      // mnuCascade
      // 
      this.mnuCascade.Index = 0;
      this.mnuCascade.Text = "Cascase";
      this.mnuCascade.Click += new System.EventHandler(this.mnuCascade_Click);
      // 
      // mnuTileV
      // 
      this.mnuTileV.Index = 1;
      this.mnuTileV.Text = "Tile Vertical";
      this.mnuTileV.Click += new System.EventHandler(this.mnuTileV_Click);
      // 
      // mnuTileH
      // 
      this.mnuTileH.Index = 2;
      this.mnuTileH.Text = "Tile Horizontal";
      this.mnuTileH.Click += new System.EventHandler(this.mnuTileH_Click);
      // 
      // mnuMinimizeAll
      // 
      this.mnuMinimizeAll.Index = 3;
      this.mnuMinimizeAll.Text = "Minimize All";
      this.mnuMinimizeAll.Click += new System.EventHandler(this.mnuMinimizeAll_Click);
      // 
      // imgButtons
      // 
      this.imgButtons.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
      this.imgButtons.ImageSize = new System.Drawing.Size(16, 16);
      this.imgButtons.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imgButtons.ImageStream")));
      this.imgButtons.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // ToolBar1
      // 
      this.ToolBar1.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
      this.ToolBar1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      this.ToolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
                                            this.cmdNew,
                                            this.cmdClose});
      this.ToolBar1.DropDownArrows = true;
      this.ToolBar1.ImageList = this.imgButtons;
      this.ToolBar1.Name = "ToolBar1";
      this.ToolBar1.ShowToolTips = true;
      this.ToolBar1.Size = new System.Drawing.Size(292, 41);
      this.ToolBar1.TabIndex = 4;
      this.ToolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.ToolBar1_ButtonClick);
      // 
      // cmdNew
      // 
      this.cmdNew.ImageIndex = 0;
      this.cmdNew.Text = "New";
      // 
      // cmdClose
      // 
      this.cmdClose.ImageIndex = 1;
      this.cmdClose.Text = "Close";
      // 
      // MDIRelatives
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
      this.ClientSize = new System.Drawing.Size(292, 266);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.ToolBar1});
      this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.IsMdiContainer = true;
      this.Menu = this.MainMenu1;
      this.Name = "MDIRelatives";
      this.Text = "MDIRelatives";
      this.ResumeLayout(false);

    }
    #endregion

    [STAThread]
    static void Main() 
    {
      Application.Run(new MDIRelatives());
    }

    private string synchronizedText = "text";
    private int mdiCount = 0;

    private void ToolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
    {
      // Determine which button was clicked.
      if (e.Button == cmdNew)
      {
        // Show a new ChildForm.
        Child frmChild = new Child();
        frmChild.MdiMDIRelatives = this;
        frmChild.RefreshText(synchronizedText);

        mdiCount++;
        frmChild.Text = "MDI Child # " + mdiCount.ToString();

        frmChild.Show();
      }
      else if (e.Button == cmdClose)
      {
        // Close the active child.
        this.ActiveMdiChild.Close();
      }

    }

    public void RefreshChildren(Child sender, string text)
    {
      // Store text for use when creating a child form, or if needed later.
      synchronizedText = text;

      // Update children.
      foreach (Child frm in this.MdiChildren)
      {
        if (frm != sender)
        {
          frm.RefreshText(text);
        }
      }
    }

    private void mnuMinimizeAll_Click(object sender, System.EventArgs e)
    {
      foreach (Form frm in this.MdiChildren)
      {
        frm.WindowState = FormWindowState.Minimized;
      }

    }

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

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

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


  }
}
//===========================================================
//===========================================================

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace MDIRelatives
{
  /// <summary>
  /// Summary description for Child.
  /// </summary>
  public class Child : System.Windows.Forms.Form
  {
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public Child()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if(components != null)
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.textBox1 = new System.Windows.Forms.TextBox();
      this.SuspendLayout();
      // 
      // textBox1
      // 
      this.textBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right);
      this.textBox1.Location = new System.Drawing.Point(8, 8);
      this.textBox1.Multiline = true;
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(212, 108);
      this.textBox1.TabIndex = 1;
      this.textBox1.Text = "textBox1";
      this.textBox1.TextChanged += new System.EventHandler(this.TextBox1_TextChanged);
      // 
      // Child
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
      this.ClientSize = new System.Drawing.Size(228, 126);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.textBox1});
      this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.Name = "Child";
      this.Text = "Child";
      this.ResumeLayout(false);

    }
    #endregion

    internal System.Windows.Forms.TextBox textBox1;


    private bool isUpdating;

    private void TextBox1_TextChanged(object sender, System.EventArgs e)
    {
      if (this.MdiParent != null && !isUpdating)
      {
        // The reference to the MDI parent must be converted to the appropriate
        // form class in order to access the custom RefreshChildren() method.
        ((Parent)this.MdiParent).RefreshChildren(this, textBox1.Text);
      }

    }
    public void RefreshText(string text)
    {
      // Disable the event to prevent an endless string of updates.
      isUpdating = true;

      // Update the control.
      textBox1.Text = text;

      // Re-enable the event handler.
      isUpdating = false;
    }

  }
}



           
       








MDIRelatives.zip( 38 k)

Related examples in the same category

1.Set MDI parent windowSet MDI parent window
2.Maximized MDI window at startupMaximized MDI window at startup
3.MDI formMDI form
4.Form OwnershipForm Ownership
5.MDI BasicsMDI Basics
6.MDI and DockMDI and Dock
7.MdiLayout.Cascade MdiLayout.Cascade
8.MdiLayout.TileHorizontal MdiLayout.TileHorizontal
9.MdiLayout.TileVerticalMdiLayout.TileVertical