This article reprinted: http://www.csframework.com/archive/2/arc-2-20110805-1771.htm
The focus of this article is to discuss how to call methods between "forms" and "forms", and how to call each other between "MDI parent form" and "Chilid subform.
C # Call a method in the parent form in the child form (or call a method between multiple forms)
It seems like a simple functional requirement. In fact, many beginners cannot handle it well, so many friends will write it like this:
C # Code:
// The parent form is frmParent and the child form is frmChildA.
// Open the child form in the parent form
FrmChildA child = new frmChildA ();
Child. MdiParent = this;
Child. Show ();
// Subform Method for parent form adjustment:
// Incorrect call !!!!!!!!
(This. MdiParent as frmParent). ParentFoo ();
// Source: C/S framework Network (www.csframework.com) QQ: 1980854898
Do you know where the error is? The error is caused by strong dependency! If the parent form and child form appear to be correct in the same module, this reverse reference is feasible in the same module, but the program cannot write it like this, you wrote it to death! Solidified! Assuming that our project is constantly expanding, We need to separate the parent form from the child form in different modules. This code is complete! The parent form module must reference the child form module, while the child form must use the frmParent class and reference the parent form module! At this time, it constitutes a two-way reference, and compilation fails, so the program writing is dead!
Is there any way to remove this dependency? The solution is to use interfaces to remove dependencies!
Let's change the program:
C # Code:
/// <Summary>
/// Main form Interface
/// </Summary>
Public interface IMdiParent
{
Void ParentFoo ();
}
/// <Summary>
/// Subform Interface
/// </Summary>
Public interface IMyChildForm
{
Void Foo ();
}
Code of the main form:
C # Code:
/// <Summary>
/// Main form to implement the IMdiParent Interface
/// </Summary>
Public partial class frmParent: Form, IMdiParent
{
Public frmParent ()
{
InitializeComponent ();
}
Private void form1ToolStripMenuItem_Click (object sender, EventArgs e)
{
// Open the subform
FrmChildA child = new frmChildA ();
Child. MdiParent = this;
Child. Show ();
}
Private void menuCallFoo_Click (object sender, EventArgs e)
{
// Call the Foo () method of the sub-Form
Form activedChild = this. ActiveMdiChild;
If (activedChild! = Null) & (activedChild is IMyChildForm ))
(ActivedChild as IMyChildForm). Foo ();
}
# Region IMdiParent Member
Public void ParentFoo ()
{
MessageBox. Show ("call" this. GetType (). FullName ". ParentFoo () method! ");
}
# Endregion
}
// Source: C/S framework Network (www.csframework.com) QQ: 1980854898
Subform code:
C # Code:
/// <Summary>
/// Subform to implement the IMyChildForm Interface
/// </Summary>
Public partial class frmChildA: Form, IMyChildForm
{
Public frmChildA ()
{
InitializeComponent ();
}
# Region IMyChildForm Member
Public void Foo ()
{
MessageBox. Show ("call" this. GetType (). FullName ". Foo () method! ");
}
# Endregion
Private void btnParentFoo_Click (object sender, EventArgs e)
{
// Call the ParentFoo () method of the parent form
If (this. MdiParent! = Null) & (this. MdiParentis IMdiParent ))
(This. MdiParent as IMdiParent). ParentFoo ();
}
Private void btnErrCall_Click (object sender, EventArgs e)
{
// Incorrect call
(This. MdiParent as frmParent). ParentFoo ();
}
// Source: C/S framework Network (www.csframework.com) QQ: 1980854898
Implementation ideas:
The module where the frmParent form is located depends on the module where frmChildA is located, while frmChildA only depends on the IMdiParent interface. This is exactly the Dependency inversion principle described in agile software development. Finally, we deploy the IMdiParent interface in a Common module. In fact, frmParent and frmChildA only need to rely on the Common module.