C # how to access the methods and variables in the parent form using the winform subform

Source: Internet
Author: User

This is suddenly used in today's work, but I have never touched it before! However, with the help of experienced colleagues, the problem was quickly solved. You can use the following methods:

1. When a child form object is constructed in the parent form, the parent form is passed over:

For example, FrmSub frm = new FrmSub (this); // this indicates the parent form

2. Change the variables and methods to be accessed in the parent form to public.

3. Define the global variable of a parent form in the child form class:

For example, private dynamic m_parentContainer; // you can use dynamic to dynamically identify the type of the variable.

4. Add reference Microsoft. CSharp. dll

5. directly use m_parentContainer. variable name or m_parentContainer. method name for access.

If (m_ParentContainer.mFrm2BaseLibDataEdite = null | m_ParentContainer.mFrm2BaseLibDataEdite.IsDisposed) // mFrm2BaseLibDataEdite is the parent form variable
{
M_ParentContainer.mFrm2BaseLibDataEdite = new frm2BaseLibDataEdite ();
If (m_ParentContainer.mFrm2BaseLibDataEdite.IsDisposed = false)
{
M_ParentContainer.fnAddChildFormToMDIForm (m_ParentContainer.mFrm2BaseLibDataEdite); // fnAddChildFormToMDIForm () is the parent form Method
M_ParentContainer.mFrm2BaseLibDataEdite.Show ();
}
}
Else {
M_ParentContainer.mFrm2BaseLibDataEdite.WindowState = FormWindowState. Maximized;
M_ParentContainer.mFrm2BaseLibDataEdite.Activate ();
M_ParentContainer.mFrm2BaseLibDataEdite.Show ();
}

Related Article

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.