C # method for calling the parent form

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.