C # parent, to pass values between subform

This is a common problem, Baidu too many methods, and I will not speak right or wrong. Just want to introduce the most simple and straightforward method

Here are two ways:

  1, no-argument

   2, reference

premise:

  1, no matter what method between, form traditional values, the method to be invoked, control functions, etc., must first qualifier is set to Public

  2, the new parent, child two forms

    The main form Main:

 

 

   Subform Child:

 

 

 

First, the no-argument (most people like to use this)

Only a little introduction: Owner property, VS explanation: Gets or sets the form that owns this form    , for species popular saying, that it supports reading and modification.

Main code behind:

namespace MainChild 
{ 
    public partial class the Main: Form1 
    { 
        public the Main () 
        { 
            the InitializeComponent (); 
        } 
        /// <Summary> 
        /// value is called 
        /// </ Summary> 
        public String callStr  {  GET {return callTxt.Text .trim ();}  sET = {callTxt.Text value;}} Private void ChildFrmBtn_Click (Object SENDER, EventArgs E) = {new new Child Child Child (); // Owner sense, the official explanation: Gets or sets have this window child.Owner the this body form = ; child.Show ();}}}

Child code behind:

namespace MainChild 
{ 
    public partial class Child: Form1 
    { 
        /// <Summary> 
        /// main form defined 
        /// </ Summary>         the Main main; 
        public Child ()  {  the InitializeComponent ();  }  /// <Summary>  / // Get the value of the parent form  /// </ Summary>  /// <param name = "SENDER"> </ param>  /// <param name = "E"> </ param>  Private void GetCallBtn_Click (Object SENDER , EventArgs E)  {  // instantiate: obtaining form main form  main = (main) this

            .Owner; GetCallTxt.Text = main.callStr;} /// <Summary> /// set the value of the parent form /// </ summary> /// <param name = "sender"> </ param> // / <param name = "E"> </ param> Private void ModifyBtn_Click (Object SENDER, EventArgs E) {// instantiate: acquiring the main form = form main (the main) the this .Owner; main.callStr = ModifyTxt.Text.Trim ();}}}

Simple bar, read and set subform, 2 lines of code, operating results:

 

 Second, there is a reference (it can be said is the constructor, I personally like to use this code less ha ha !!!)

    Form is set to the same, except that the reference mass

Main.cs

public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 被调用的值
        /// </summary>
        public string callStr
        {
            get { return callTxt.Text.Trim(); }
            set { callTxt.Text = value; }
        }

        private void ChildFrmBtn_Click(object sender, EventArgs e) { Child child = new Child(this);//这里this child.Show(); } }

Unlike no parameters, when there is a reference in the necessary stated structure, the acquisition of mass participation

Child.cs

partial class public Child: Form1 
    { 
        /// <Summary> 
        /// main form defined 
        /// </ Summary>         the Main main; 
        public Child (the Main FRM) 
        { 
            the InitializeComponent (); 
            main = FRM; // this is to obtain parameter  }  /// <Summary>  /// Gets the value of the parent form  /// </ Summary>  /// <param name = "SENDER"> </ param>  /// <param name = "E"> < / param>  Private void GetCallBtn_Click (Object SENDER, EventArgs E)  {  GetCallTxt.Text =
 main.callStr; } /// <summary> /// 设置父窗体值 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ModifyBtn_Click(object sender, EventArgs e) { main.callStr = ModifyTxt.Text.Trim(); } }

   Well, the results of realization of the two methods are the same, it depends on personal likes!

Guess you like

Origin www.cnblogs.com/Craft001wen/p/11505156.html