Value passing for Winform development, form for winform Development

Source: Internet
Author: User

Value passing for Winform development, form for winform Development

Data transmission between Winform forms is a necessary technology for development. The following describes several typical value passing methods.

1. the constructor transfers values, but this method is unidirectional (recommended)

Upload the code, first pass the value

Private void button2_Click (object sender, EventArgs e) {Form3 fr3 = new Form3 ("value to be passed"); fr3.ShowDialog ();}

Value. By the way, we need to reload a Form3 constructor and display the obtained value.

 public Form3(string canshu)        {            InitializeComponent();            label1.Text = canshu;        }

Is that simple

2. static variable value transfer (not recommended)

You can declare static variables where you need them, such as a separate class or Form. For example, we declare static variables in Form2 here.

 public static string xvalue;
Private void button2_Click (object sender, EventArgs e) {xvalue = "value to be passed xvalue"; Form3 fr3 = new Form3 (); fr3.ShowDialog ();}

Assign values first, and then set values in Form3.

Public Form3 () {InitializeComponent (); label1.Text = Form2.xvalue; // Form2 is actually a class. You can directly obtain the static value. If the static variable xvalue is defined in other classes, to replace Form2}


3. Pass the value through the common property

Create a common attribute in the Form to be opened, and assign a value in the call window. For example, if Form2 starts Form3, it will pass a value to the yvalue of Form3.

(1) define common attributes in Form3

public string yvalue {            get             {                return label1.Text.ToString();            }            set             {                label1.Text = value;            }        }

(2) Start Form3 in Form2 and pass the value

Private void button2_Click (object sender, EventArgs e) {Form3 fr3 = new Form3 (); fr3.yvalue = "value to be passed"; fr3.ShowDialog ();}

4. Pass the value through the Owner attribute

(1) declare a public variable in the caller Form2, assign a value, and set the Owner of the Form3 to be started.

Public string xvalue; private void button2_Click (object sender, EventArgs e) {xvalue = "Form2 value to be passed"; Form3 fr3 = new Form3 (); fr3.Owner = this; fr3.ShowDialog ();}

(2) set the value in the form Form3.

 private void Form3_Load(object sender, EventArgs e)        {            Form2 fr2 = (Form2)this.Owner;            label1.Text = fr2.xvalue;        }

This method actually passes Form2 to Form3, so Form3 can get all the public variables and attributes of Form2.

5. Delegate value transfer (recommended)

The delegated value is mainly used to transmit values to the parent form in the subform, that is, the preceding Form3 transmits values to Form2.

(1) Declare the Commission in Form3 first

Public delegate void PutHandler (string text); public PutHandler putTextHandler; // delegate object private void button#click (object sender, EventArgs e) {if (putTextHandler! = Null) {putTextHandler (textBox1.Text. ToString ());}}

(2) bind a delegate event in Form2

 public void getValue(string strV)          {             this.textBox1.Text = strV;         }               private void button1_Click(object sender, EventArgs e)        {            Form3 fr3 = new Form3();            fr3.putTextHandler = getValue;            fr3.ShowDialog();                  }

Of course there are other methods of passing values, and interested friends can study it again.

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: 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.