There are several methods on the network, which are summarized as follows:
Call form (parent): FormFather, called form (child): FormSub
Method 1: All rights
// FormFather:
// A public refresh method is required
Public void Refresh_Method ()
{
//...
}
// When FormSub is called, set the FormSub owner to FormFather.
FormSub f2 = new FormSub ();
F2.Owner = this;
F2.ShowDialog ();
// FormSub:
// When the caller (parent) needs to be refreshed
FormFather f1;
F1 = (FormFather) this. Owner;
F1.Refresh _ Method ();
Method 2: Self-Transmission Method
// FormFather:
// A public refresh method is required
Public void Refresh_Method ()
{
//...
}
FormSub f2 = new FormSub ();
F2.ShowDialog (this );
// FormSub:
Private FormFather p_f1;
Public FormSub (FormFather f1)
{
InitializeComponent ();
P_f1 = f1;
}
// When refreshing
P_f1.Refresh_Method ();
Method 3: Limit Method
// FormFather:
// A public refresh method is required
Public void Refresh_Method ()
{
//...
}
// Call time
FormSub f2 = new FormSub ();
F2.P _ F1 = this;
F2.Show ();
// FormSub:
Private FormFather p_f1;
Public FormFather P_F1
{
Get {return p_f1 ;}
Set {p_f1 = value ;}
}
// When refreshing
P_f1.Refresh_Method ();
Method 4: Delegation
// FormFather:
// A public refresh method is required
Public void Refresh_Method ()
{
//...
}
// Call time
FormSub f2 = new FormSub ();
F2.ShowUpdate + = new DisplayUpdate (Refresh_Method );
F2.Show ();
// FormSub:
// Declare a delegate
Public delegate void DisplayUpdate ();
// Declare the event
Public event DisplayUpdate ShowUpdate;
// Refresh is placed in the event to be refreshed
If (ShowUpdate! = Null) ShowUpdate ();
// After subforms are submitted
Private void btnOK_Click (object sender, EventArgs e)
{
This. DialogResult = DialogResult. OK;
This. Close ();
}
// Determine the subform
If (form. ShowDialog () = DialogResult. OK)
{
Refresh the DataGRIDVIEW data in the parent form
}
Reprinted from: http://hi.baidu.com/bj_cjz/blog/item/d29691355673448ea71e1253.html