你的位置:首页 > ASP.net教程

[ASP.net教程]winform窗体间传值


1.通过构造函数

特点:传值是单向的(不可以互相传值),实现简单实现代码如下:在窗体Form2中   int value1;string value2;public Form2 ( int value1 , string value2 ){  InitializeComponent ( );  this.value1 = value1;  this.value2 = value2;}在窗体Form1中这样调用new Form2 ( 111 , "222" ).Show ( ); //这样就把111,"222",这2个值传送给了Form2

2.通过静态变量

特点:传值是双向的,实现简单实现代码如下:在一个app类中定义一个静态成员valuepublic class app{ public static string value;}在窗体Form1中这样调用app.value = "f2";    //给静态成员赋值new Form2 ( ).Show ( ); //显示Form2在窗体Form2中   this.Text = app.value; //取回app.value的值app.value = "Form2";  //给app.value赋值,以便其他窗体调用

View Code

3.通过窗体的公有属性值

特点:实现简单实现代码如下:在窗体Form2中定义一个公有属性Form2Value,获取和设置textBox1的文本值public string Form2Value{  get  {    return this.textBox1.Text;  }  set  {    this.textBox1.Text = value;  }}在窗体Form1中这样调用Form2 f2 = new Form2 ( );f2.Form2Value = "Ok";   //给Form2的textBox1赋值Okf2.ShowDialog ( );

View Code

4.通过窗体的公有属性值和Owner属性

特点:实现简单,灵活实现代码如下:在窗体Form1中public int Form1Value = 1;Form2 f2 = new Form2 ( );f2.ShowDialog ( this );    //把Form1作为Form2的所有者传递给Form2在窗体Form2中//Form2的所有者是Form1Form1 f1 = ( Form1 ) this.Owner;//取到Form1的值是1MessageBox.Show ( f1.Form1Value .ToString ( ) );//给Form1的Form1Value赋值222f1.Form1Value = 222;

View Code

5.通过窗体的公有属性值和Application.OpenForms属性

说明:Application.OpenForms属性:获取属于应用程序的打开窗体的集合。(此属性在 .NET Framework2.0版中)实现代码如下:在窗体Form1中public int Form1Value = 1;Form2 f2 = new Form2 ( );f2.Show ( );在窗体Form2中string formName = "Form1";Form fr = Application.OpenForms [ formName ];if ( fr != null ){  Form1 f1 = ( Form1 ) fr;  //取到Form1的值是1  MessageBox.Show ( f1.Form1Value.ToString ( ) );  //给Form1的Form1Value赋值222  f1.Form1Value = 222;}

6.通过事件

实现代码如下:在窗体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;}

7:通过控件赋值

 public partial class Form1 : Form  {    public Form1()    {      InitializeComponent();    }    private void button1_Click(object sender, EventArgs e)    {      Form2 frm2 = new Form2();      frm2.Show(this);//或 frm2.ShowDialog(this);      ////或者      //Form2 frm2 = new Form2();      //frm2.Owner = this;      //frm2.Show();//或 frm2.ShowDialog();    }  }  public partial class Form2 : Form  {    public Form2()    {      InitializeComponent();    }    private void button2_Click(object sender, EventArgs e)    {      Form1 frm1 = (Form1)this.Owner;   //注意 如果textBox1是放在panel1中的 则先找panel1 再找textBox1    如果是Panel中的textbox1   //Panel pl = (Panel)(frm1.Controls["panel1"]);      // ((TextBox)pl.Controls["textBox1"]).Text = this.textBox2.Text;      ((TextBox)frm1.Controls["textBox1"]).Text = this.textBox2.Text;      this.Close();    }  }

View Code

转自http://www.cnblogs.com/xiaofengfeng/p/3182056.html