简体   繁体   English

C#-两种形式之间的数据传输

[英]C#- Data transfer between two forms

I have two forms. 我有两种形式。 First one has one textbox.Second one has a devexpress data grid. 第一个具有一个文本框,第二个具有devexpress数据网格。 I want to achieve that: first click a button and form2 opens. 我想要实现这一点:首先单击一个按钮,然后打开form2。 if I click a row in the data grid in form2, this value should be shown inside the textbox in form1.(form1 is already opened.) ı ma beginner. 如果在form2中单击数据网格中的一行,则此值应显示在form1的文本框中。(form1已打开。) thanks for your help. 谢谢你的帮助。

Form1 frm1 = new Form1();
frm1.textBox1.Text = gridView1.GetFocusedRowCellValue("ID").ToString();
frm1.Show(); 

when I do that, a new form opens. 当我这样做时,将打开一个新表格。 I dont want to open a new form. 我不想打开一个新表格。 Form1 is already opened. Form1已经打开。 I want to add values to its textbox. 我想在其文本框中添加值。

Pass form1 as a reference to form2: 传递form1作为对form2的引用:

In your button click handler on form 1 to open form 2 在您的按钮上单击表格1上的处理程序以打开表格2

private void Button_Click(object sender, EventArgs e)
{
    Form2 frm2 = new Form2(this);
    frm2.Show();
}

form2 code 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();

you can try something like this. 您可以尝试这样的事情。

When you click on frm1.SimpleButton this function will be called a show your Form2. 当您单击frm1.SimpleButton时,此函数将称为show Form2。 On Form2 you have to set InnerProperty (from your datagrid?) a when you close Form2 you can use this property. 在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;
  }
}

Finally, I solved the problem. 最后,我解决了这个问题。

Application.OpenForms["form1"].Controls["textBox1"].Text = 
               gridView1.GetFocusedRowCellValue("ID").ToString();

Thanks for help. 感谢帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM