C # Winform Subform access controls and properties of the parent form _c# Tutorial

Source: Internet
Author: User

Today in a contact management of C # design, with this problem, I need to upload the values from the TextBox in the parent form to the subform and query the database, and I used the new parent form (). TextBox.Text; To carry out the value transfer, but without eggs, after many experiments, A simpler solution was found:

A, subform calls a static variable of the parent form

Parent form: Logout

Child form: Affirm

Parent form text box: Tb_logout_username

public partial class Logout:form
{

  //define a static variable that holds the value of the text box in the parent form public

  static string tb_logoutname;

  An event that instantiates a subform

  private void Btt_logout_click (object sender, EventArgs e)
  {

    //Gets the value of the text box in the parent form

    tb_ Logoutname = Tb_logout_username.text;
    Affirm aff = new affirm ();
    Aff. Show ();

  }

Next you can call it in the subform directly: the parent form. Variable

namely: Logout.tb_logoutname

This method is more tricky, it should be found first to get the parent form method, and then operate on it.

Second, pass the parent form as a property to a subform

Define the parent form field for public in your subform, such as:

public class Affirm:form
{public
  Logout mylogout;
}

You can then set its value in the parent form, such as:

public partial class Logout:form
{
 
  //define a static variable that holds the value of the text box in the parent form public
 
  static string tb_logoutname;
 
  An event that instantiates a subform
 
  private void Btt_logout_click (object sender, EventArgs e)
  {
 
    //Gets the value of the text box in the parent form
 
    //tb_ Logoutname = Tb_logout_username.text;
    Affirm aff = new affirm ();
    Aff. Mylogout=this;
    Aff. Show ();
 
  }
 

In this way, the members that are exposed in the parent form can be accessed in the subform, using.

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.