1. 程式人生 > >C# 子窗體中呼叫父窗體中的方法(或多窗體之間方法呼叫)

C# 子窗體中呼叫父窗體中的方法(或多窗體之間方法呼叫)

C# Code:

///<summary>
/// 主窗體,實現IMdiParent介面
///</summary>
public partial class frmParent : Form, IMdiParent
{
   public frmParent()
   {
      InitializeComponent();
   }
   
   private void form1ToolStripMenuItem_Click(object sender, EventArgs e)
   {
      //開啟子窗體 
      frmChildA child = new frmChildA();
      child.MdiParent = this
;
      child.Show();
   }
   
   private void menuCallFoo_Click(object sender, EventArgs e)
   {
      //呼叫子窗體的Foo()方法 
      Form activedChild = this.ActiveMdiChild;
      if ((activedChild != null) && (activedChild is IMyChildForm))
         (activedChild as IMyChildForm).Foo();
   }
   
   #region
 IMdiParent 成員
   
   public void ParentFoo()
   {
      MessageBox.Show("呼叫" this.GetType().FullName ".ParentFoo()方法!");
   }
   
   #endregion
}

//來源:C/S框架網(www.csframework.com) QQ:1980854898