![](https://i-blog.csdnimg.cn/blog_migrate/dddca0cf32ae8c3c767b1a628477eee0.jpeg)
一共有4种实现方法
- Using constructor
- Using objects
- Using properties
- Using delegates
这里主要介绍方法3、4
方法3:
Step 1: Add a property in form1 to retrieve value from textbox.
{
get { return this .txtBox1.Text.ToString().Trim();}
}
Step 2: Add a property in form2 to set the labels’ text
{
set { this .label1.Text = value; }
}
Step 3: In form1’s button click event handler add the following code.
{
Form2 frm2 = new Form2();
frm2.Show();
frm2._recevicetxt = _sendtxt;
}
方法4:
Step 1: Add a delegate signature to form1 as below
Step 2: In form1’s button click event handler instantiate form2 class and delegate. Assign a function in form2 to the delegate and call the delegate as below
{
Form2 frm = new Form2();
delPassData del = new delPassData(frm.funData);
del( this .textBox1);
frm.Show();
}
Step 3: In form2, add a function to which the delegate should point to. This function will assign textbox’s text to the label.
{
label1.Text = txtForm1.Text;
}
详细介绍请参考:http://www.codeproject.com/useritems/pass_data_between_forms.asp
FrmM: txt1、txt2、txt3的Modify设为:Public
private void btn1_Click(object sender, EventArgs e)
{
FrmS fs = new FrmS();
fs.Owner = this;//Owner:拥有当前窗体的窗体
fs.ShowDialog();
}
FrmS:
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
FrmM fm = (FrmM)this.Owner;
fm.txt1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
fm.txt2.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
fm.txt3.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
this.Close();
}