简体   繁体   中英

C#- Data transfer between two forms

I have two forms. First one has one textbox.Second one has a devexpress data grid. I want to achieve that: first click a button and form2 opens. 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. 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. I want to add values to its textbox.

Pass form1 as a reference to form2:

In your button click handler on form 1 to open form 2

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

form2 code

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. On Form2 you have to set InnerProperty (from your datagrid?) a when you close Form2 you can use this property.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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