[英]C#- Data transfer between two forms
我有两种形式。 第一个具有一个文本框,第二个具有devexpress数据网格。 我想要实现这一点:首先单击一个按钮,然后打开form2。 如果在form2中单击数据网格中的一行,则此值应显示在form1的文本框中。(form1已打开。) 谢谢你的帮助。
Form1 frm1 = new Form1();
frm1.textBox1.Text = gridView1.GetFocusedRowCellValue("ID").ToString();
frm1.Show();
当我这样做时,将打开一个新表格。 我不想打开一个新表格。 Form1已经打开。 我想在其文本框中添加值。
传递form1作为对form2的引用:
在您的按钮上单击表格1上的处理程序以打开表格2
private void Button_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this);
frm2.Show();
}
form2代码
private Form1 frm1;
// constructor (pass frm1 as reference)
public Form2(Form1 frm1) {
this.frm1 = frm1;
}
//put this in your event handler for a row click in the grid
frm1.textBox1.Text = gridView1.GetFocusedRowCellValue("ID").ToString();
您可以尝试这样的事情。
当您单击frm1.SimpleButton时,此函数将称为show Form2。 在Form2上,您必须设置InnerProperty(从您的数据网格?),当您关闭Form2时,可以使用此属性。
private void SimpleButton_Click(object sender, EventArgs e)
{
using (Form2 frm = new Form2())
{
frm.InnerProperty = "default text";
DialogResult result = frm.ShowDialog();
SimpleButton.Text = frm.InnerProperty;
}
}
最后,我解决了这个问题。
Application.OpenForms["form1"].Controls["textBox1"].Text =
gridView1.GetFocusedRowCellValue("ID").ToString();
感谢帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.