The first method: the entire form as the value passed Form1 Form2
form1 button1 pop from2,
from2 numericUpDown1 value changes, so that the value of change form1 textBox1.
Form1 Code:
// The first method: pass the entire form as the value of Form2 Private void the button1_Click ( Object SENDER, EventArgs E) { Form frm2 = new Form2(this); frm2.ShowDialog(); } // change value textBox1 public void ChangeText ( String S) { this.textBox1.Text = s; }
Form2 Code:
// will pass over all Form1 assigned to f1, so that you can call in here Form1 Private Form1 f1; public Form2 (Form1 frm1) { InitializeComponent(); f1 = frm1; } private void numericUpDown1_ValueChanged(object sender, EventArgs e) { // call Form1 in changetext event f1.ChangeText (numericUpDown1.Value.ToString ()); }
The second method: delegate to the event
form1 button1 pop from2,
from2 numericUpDown1 value changes, so that the value of change form1 textBox1,
from2 numericUpDown1 empty value, so that the value of the empty form1 textBox1.
Form1 Code:
// 2. Registration Event Private void button1_Click ( Object SENDER, EventArgs E) { Frm3 Form3 = new new Form3 (); // write the + =, pressing the Tab key twice will automatically generate frm3_changed, frm3_empty two events frm3.UpdateTextValueEvent + = new new Form3.ChangeTextValueDelegate (frm3_changed); // event value change frm3 + = .EmptyTextValueEvent new new Form3.ChangeTextValueDelegate (frm3_empty); // event empty value frm3.ShowDialog (); } // change the value of the event Private void frm3_changed ( String S) { this.textBox1.Text = s; } // clear the value of the event Private void frm3_empty ( String S) { this.textBox1.Text = ""; }
Form3 Code:
// delegate 1. Define two parameters and events public the delegate void ChangeTextValueDelegate ( String S); public Event ChangeTextValueDelegate UpdateTextValueEvent; public Event ChangeTextValueDelegate EmptyTextValueEvent; //3.传值 private void numericUpDown1_ValueChanged(object sender, EventArgs e) { UpdateTextValueEvent(numericUpDown1.Value.ToString()); } private void button1_Click(object sender, EventArgs e) { EmptyTextValueEvent(numericUpDown1.Value.ToString()); }