文章出處

在窗體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; 
}

  


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()