C#Winform窗体中传值

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

  

posted @ 2016-11-21 13:36  会弹猫的吉他  阅读(284)  评论(0编辑  收藏  举报