Hi
I will supply some sample code...
create a test app, with two forms, and cut and paste the following code into
the forms replacing all of the existing code..
what is illustrated is that a parent carries a reference to its active
child, and the child carries a reference to its parent, therefore when that
form becomes active the parents active child can be set.
Form1
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace MDI_Test
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// 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.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "Say Hello";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(448, 406);
this.IsMdiContainer = true;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private Form2 m_fCurrent;
public Form2 fCurrent
{
get
{
return m_fCurrent;
}
set
{
m_fCurrent = value;
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
Form2 f2 = new Form2();
f2.MdiParent = this;
f2.Show();
f2.f1 = this;
f2.Text = "Bill";
Form2 f3 = new Form2();
f3.MdiParent = this;
f3.Show();
f3.f1 = this;
f3.Text = "Mary";
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
fCurrent.SayHello();
}
}
}
Form 2
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace MDI_Test
{
/// <summary>
/// Summary description for Form2.
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form2()
{
//
// 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()
{
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
this.Enter += new System.EventHandler(this.Form2_Enter);
}
#endregion
private Form1 m_f1;
public Form1 f1
{
get
{
return m_f1;
}
set
{
m_f1 = value;
}
}
private void Form2_Load(object sender, System.EventArgs e)
{
}
private void Form2_Enter(object sender, System.EventArgs e)
{
this.f1.fCurrent = this;
this.f1.Text = this.f1.fCurrent.Text;
}
public void SayHello()
{
MessageBox.Show("Hello I am a - " + this.Name + " My Name Is " +
this.Text);
}
}
}
//end of code
what this demonstrates is the creation of two forms of the same type, and
calling the method sayhello() on the active form.
remember and switch the active forms before trying the menu option, or there
is no active child set...