0
votes

I have 2 forms. One have a textbox to show name of chosen Customer and one have datagridview to show list of customers. When i click on textbox the second form will open. I want to know is it possible to double click on datagridview cell then the text of textbox change by datagridview selected cell on first form? Here is my codes.

I wrote this code to open form2:

private void textBox1_Click(object sender, EventArgs e) 
{
    frmCustomer customer = new frmCustomer();            
    customer.ShowDialog();
}

and in form2 :

private void dataGridView1_CellDoubleClick(object sender,DataGridViewCellEventArgs e)
{
   string name =  dataGridView1.CurrentRow.Cells["clmName"].Value.ToString();
   form1 f = new form1();
   f.txtCustomer.Text = name;
   this.close();
 }

there is nothing in textbox when form2 closed.

any Help? Thanks a milion

2
You create a new form1 instance , instead pass a referenceSybren

2 Answers

0
votes

You can try passing Form1 as a Parameter when creating the Form2.

private void textBox1_Click(object sender, EventArgs e) 
{
    frmCustomer customer = new frmCustomer(this); // this represents the Form1           
    customer.ShowDialog();
}

then on the form2

private Form1 frm_1;
public Form2(Form1 frm)
{
    InitializeComponent();
    frm_1 = frm;
}

private void dataGridView1_CellDoubleClick(object sender,DataGridViewCellEventArgs e)
{
   string name =  dataGridView1.CurrentRow.Cells["clmName"].Value.ToString();
   frm_1.txtCustomer.Text = name;
   this.close();
 }

this will do the work.

0
votes

Closing the second form does not dispose it off. First form will still have access to its public members.
The second form can have a public property which can be accessed by first form after it is closed.

Try the following: first form should contain:

private void textBox1_Click(object sender, EventArgs e)
{
    frmCustomer customer = new frmCustomer();
    customer.ShowDialog();
    textBox1.Text = customer.Name;
    customer.Dispose();
}

second form should contain:

public string Name { get; private set; }

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
   Name = dataGridView1.CurrentRow.Cells["clmName"].Value.ToString();
   this.close();
}