7.12 Accessing MDI Child Windows

 <  Day Day Up  >  

You want to manipulate the child forms from a parent form in a MDI application.


Technique

To access the currently active MDI child form, use the ActiveMdiChild property defined in the parent form. To enumerate all MDI children, you can use a foreach enumeration on the MdiChildren collection. Listing 7.3 demonstrates how to change the title bar of each MDI child. The application also shows how to access individual controls within each MDI child. Figure 7.8 shows the result of running the application in Listing 7.3.

Listing 7.3 Changing MDI Child Title Bars
 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Text; namespace _12_MDIChild {     public class Form1 : System.Windows.Forms.Form     {         private System.ComponentModel.Container components = null;         ChildForm[] childForms = new ChildForm[3];         public Form1()         {             InitializeComponent();             for( int i = 0; i < 3; i++ )             {                 childForms[i] = new ChildForm();                 childForms[i].MdiParent = this;                 childForms[i].Show();             }         }         protected override void Dispose( bool disposing )         {             if( disposing )             {                 if (components != null)                 {                     components.Dispose();                 }             }             base.Dispose( disposing );         }         #region Windows Form Designer generated code         private void InitializeComponent()         {             //             // Form1             //             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);             this.ClientSize = new System.Drawing.Size(292, 266);             this.IsMdiContainer = true;             this.Name = "Form1";             this.Text = "Form1";             this.MdiChildActivate +=                 new System.EventHandler(this.Form1_MdiChildActivate);         }         #endregion         private void Form1_MdiChildActivate(object sender, System.EventArgs e)         {             StringBuilder childTitle = new StringBuilder();             foreach( Form child in this.MdiChildren )             {                 childTitle.Append(child.Text);                 childTitle.Replace( "- Active", "" );                 if( this.ActiveMdiChild == child )                 {                     child.ActiveControl.Text = "Active Child";                     child.Text = childTitle.ToString() + "- Active";                 }                 else                 {                     child.Controls[0].Text = "Not Active Child";                     child.Text = childTitle.ToString();                 }                 childTitle.Remove(0, childTitle.Length);             }         }         [STAThread]         static void Main()         {             Application.Run(new Form1());         }     } } 
Figure 7.8. Each MDI child's title bar changes based on its active state.

graphics/07fig08.gif

Comments

In Listing 7.3, you can see how to handle the event that is fired whenever a new MDI child is activated. The event, MdiChildActivate , does not have any associated event arguments, but you can determine which child was activated by accessing the ActiveMdiChild property from the parent form. If you look at the result in Figure 7.8, you can see that the main form can change each MDI child's window caption. Furthermore, the parent can also manipulate each child form's control collection by enumerating all child forms using the MdiChildren collection.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net