WinForm窗体之间传值。

最近看了一个关于ListView的例子,在ListView选中项时,自动弹出一个新窗体显示选中项的数据,窗体和窗体之间怎么传值。有以下两种方法,

1》是声明一个Public类,当选中项时,将值放到类中去,再实例一个窗体时,将类传到新窗体。再从类中取出值,赋给TextBox显示,在新窗体要修改的值的时候,就不能了。

代码
ListViewItem lt= this.listView1.SelectedItems[0];
s
= new PerSon();
s.Id
= lt.Text;
s.Name
= lt.SubItems[1].Text;
s.Address
= lt.SubItems[2].Text;
Form3 fr3
= new Form3(s);
fr3.Show();

 

 

代码
public partial class Form3 : Form
{
PerSon s;
public Form3(PerSon s)
{
this.s = s;
InitializeComponent();
}

private void Form3_Load(object sender, EventArgs e)
{
this.textBox1.Text = s.Id;
this.textBox2.Text = s.Name;
this.textBox3.Text = s.Address;
}
}

2》直接将this.ListView1,传到新窗体,直接操作ListView1。

 

代码
ListViewItem lt= this.listView1.SelectedItems[0];
//s = new PerSon();
//s.Id = lt.Text;
//s.Name = lt.SubItems[1].Text;
//s.Address = lt.SubItems[2].Text;
Form3 fr3 = new Form3(this.listView1);
fr3.Show();

 

代码
//PerSon s;
ListView ls;
//ListViewItem lst;
public Form3(ListView ls)
{
this.ls = ls;
InitializeComponent();
}

private void Form3_Load(object sender, EventArgs e)
{
ListViewItem lst
= this.ls.SelectedItems[0];
this.textBox1.Text = lst.Text;
this.textBox2.Text = lst.SubItems[1].Text;
this.textBox3.Text = lst.SubItems[2].Text ;
}

private void button1_Click(object sender, EventArgs e)
{
ListViewItem lst
= this.ls.SelectedItems[0];
lst.Text
= this.textBox1.Text;
lst.SubItems[
1].Text = this.textBox2.Text;
lst.SubItems[
2].Text = this.textBox3.Text;
}

 

 

 

posted @ 2010-07-19 09:52  WIN8新人  阅读(430)  评论(0编辑  收藏  举报