文章出處
文章列表
在窗體Form2中定義公有屬性Form2Value,獲取和設置textBox1的文本值 并且還定義一個accept事件 public string Form2Value { get { return this.textBox1.Text; } set { this.textBox1.Text = value; } } public event EventHandler accept; private void button1_Click ( object sender , EventArgs e ) { if ( accept != null ) { accept ( this , EventArgs.Empty ); //當窗體觸發事件,傳遞自身引用 } } 在窗體Form1中 Form2 f2 = new Form2 ( ); f2.accept += new EventHandler ( f2_accept ); f2.Show ( ); void f2_accept ( object sender , EventArgs e ) { //事件的接收者通過一個簡單的類型轉換得到Form2的引用 Form2 f2 = (Form2) sender; //接收到Form2的textBox1.Text this.textBox1.Text = f2.Form2Value; }
文章列表
全站熱搜