Question

I have Windows form form1 where pressing Enter key opens a new form2. In that form2 there is a datagridview showing different values. Now, on pressing Enter key in form2 I get selected value of datagridview. Issue is I want to move that value to previous form1 but when I try it opens a new form and moves the value.

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        Form37 f = new Form37();
        f.a = 1;
        f.v = dataGridView1.SelectedCells[0].Value.ToString();

        f.Show();
    }
}
Was it helpful?

Solution

Your second form needs a reference to the first one:

Form37:

Edit the Form37 keydown event

private void Form37_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        Form38 f = new Form38();
        f.caller_form = this;
        this.Hide();
        f.Show();
    }
}

Form38:

Add a new variable to your Form38 class's root:

public partial class Form38 : Form
    {
       Form37 caller_form;

Edit the Form38's dataGridView1's keydown event

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        Form37 f = caller_form;
        f.a = 1;
        f.v = dataGridView1.SelectedCells[0].Value.ToString();
        f.Show();
        this.Close();
    }
}

OTHER TIPS

        //  Mimize Your From1 using
          From1.WindowState = FormWindowState.Minimized;
         //..... 

        // Key press On data Grid
        //.....


        //using this get from37 and 
    foreach (Form  frms in this.MdiChildren)
{
    if (frms is from37 )
    {
        if (frms.WindowState==FormWindowState.Minimized)
        {
            frms.WindowState = FormWindowState.Maximized;

            //Minimized current form;
            this.WindowState = FormWindowState.Minimized;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top