http://blog.csdn.net/simonllf/archive/2007/03/01/1517934.aspx
今天在网上找关于窗体间传值的方法,看了好几个,但感觉两个比较好用,现转过来以便以后方便使用
1.窗体间以对象的形式传递
假设有两个form,form1、form2和一个Class1.cs的类文件。form1是程序的开始窗体,通过form1 来调用form2 。程序如下:
Class1.cs文件的内容是
public class Class1
{
public int i;
public Class1()
{
//
// TODO:
i = 9;
}
public void modify( int u )
{
i = u;
}
}
Form1中的内容是
private Class1 ttt;
private void Form1_Load(object sender, System.EventArgs e)
{
ttt = new Class1();
Form2 temp = new Form2();
temp.Change = ttt;
temp.Show();
}
private void button1_Click(object sender, System.EventArgs e)
{
textBox1.Text = ttt.i.ToString();
}
private void button2_Click(object sender, System.EventArgs e)
{
ttt.modify( 44);
}
form2中的内容是:
private Class1 change;
public Class1 Change
{
get { return change ;}
set
{
change= value;
}
}
private void button1_Click(object sender, System.EventArgs e)
{
textBox1.Text = change.i.ToString();
}
private void button2_Click(object sender, System.EventArgs e)
{
change.modify( 98 );
}
运行程序你会发现,改form1 中的textbox值,那么form2 中change中的i 的值也会相应的变,同样form2中change的 i 值变了,那么form1 中的 ttt 中的 i 也相应的变了。就好像两个form在使用同一个数据变量一样,为什么呢?
经过思考,其实很简单,原因就在于我们在使用property传递数据时使用了同一块内存空间。(牛牛注:我想就是值传递,因为把对象作为参数就是值传递)
在传递class类型的数据时(如上),由于我们没有new一个新的实例,而是直接赋值,所以就相当于使用了引用,把上面的赋值过程改成下面这样,
private Class1 change;
public Class1 Change
{
get { return change ;}
set
{
change = new Class1();
change.i = value.i ;
}
}
那么两个form中的值相互之间就不再有什么关联了,也就是说,改其中一个,不会影响另外一个。这也给了我们一个启示,当我们想在form之间传值时,而且还想让值之间有一定的联系,那么就把这些值用class来包起来,再传。这样又清楚又省事。
如 果你传的不是类而是一般数据(int , string )等,那么这些数据在form之间是没有联系的,因为C#在定义这些数据类型时,就默认new了他们,例如:int i ; 和int i = new int() ; 是一样的,所以我以前经常在form之间传递简单的变量时,没有发现数据之间的关系,直到今天传 class 时才发现。像下面这样的 class :
class temp
{
int i ;
int[] mm;
public temp ()
{ mm = new int [10] ; }
}
在form之间传递时,变量 i 是两个窗体共用的,但是mm 却不是,其原因就是我上面讲的那样,所以利用property的这个特性,我们可以灵活的在form之间传递我们想变和不想变的值。
在这里我要加几句,这个方法其实还有一种本质上和他一样的方法,就是把窗体作为对象进行转递来达到相同的效果,只是这样的方法比较适用于两个窗体间的互动,多个窗体就不是很好用了:
(This article describes a simplified approach to allowing communication between forms without the use of events and delegates. The approach demonstrated is adequate if the application uses a single instance of a form and allows the creation of a single instance of a dialog used to pass data to the main calling form. If you need to broadcast the data generated by a dialog to multiple form listeners, you will still need to work with events and delegates; this approach is only valid if there is a one on one relationship between the two interactive forms.)这是这个方法的一点说明:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace LimitedDataXfer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnOpenForm_Click(object sender, EventArgs e)
{
Form2 f = new Form2(this);
f.Show();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace LimitedDataXfer
{
public partial class Form2 : Form
{
Form1 f;
public Form2()
{
InitializeComponent();
}
public Form2(Form1 fr1)
{
InitializeComponent();
f = new Form1();
f = fr1;
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
f.lblName.Text = textBox1.Text;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
f.lblStreet.Text = textBox2.Text;
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
f.lblCity.Text = textBox3.Text;
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
f.lblState.Text = textBox4.Text;
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
f.lblZip.Text = textBox5.Text;
}
}
}
具体可以参考这个地址:http://www.c-sharpcorner.com/UploadFile/scottlysle /LimitedDataXfer01302007012304AM/LimitedDataXfer.aspx?ArticleID=98b894ff-2a4d-42b6-8aa7-7c0b210e001f
2. 用委托的方式传递
FROM2中
public delegate void changelabel(string s);
public event changelabel change;
private void button1_Click(object sender, System.EventArgs e)
{
if(change != null)
{
change(this.textBox1.Text);
}
}
FROM 1中
private void button2_Click(object sender, System.EventArgs e)
{
frmlabel frm = new frmlabel();
frm.change += new frmlabel.changelabel(myevent);
frm.Show();
}
private void myevent(string s)
{
this.label2.Text = s;
}