Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So, I will try to explain what I want my application to do:

1) In the Main Form I have a TextBox and a DataGridView. I will insert into the TextBox what I want to search and then click F1 to open the Second Form which will display in another DataGridView.

2) I will double click into the Second Form DataGridView and that column value will be displayed into the TextBox from the Main Form.

3) After it, that TextBox is filled and depending on that value it will insert into the Main Form DataGridView that value detailed.

In the Second Form DataGridView I have the double click event with this:

What I have tried:

C#
private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
    try
    {
        DataGridViewRow dr = dataGridView1.SelectedRows[0];
        this.Hide();

        frmPrincipal frm = new frmPrincipal();

        frm.Show();

        frm.txtCarga.Text = dr.Cells[0].Value.ToString();

        frm.txtCarga.Focus();
        frm.txtCarga.SelectAll();

    }
    catch (Exception ex)
    {
        MessageBox.Show("Erro\nDetalhes: " + ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}


The issue here is that if I use the frm.Show(); it will open a new frmPrincipal form, but I already have one. If I comment the frm.Show(); the code will not be executed, but no erros displays. Basically that value will not be displayed into the TextBox.

What should I do?
Posted
Updated 1-Feb-17 3:52am
v4
Comments
[no name] 1-Feb-17 9:43am    
"it will open a new frmPrincipal form", yes because that is exactly what you told it to do.
"frm.Show(); the code will not be executed", not true at all. It will execute, you just won't be able to see it on the screen.
"What should I do?", you probably want to use your existing instance of the form instead of creating a new one.
Member 12977712 1-Feb-17 9:46am    
Could you help me with that?
[no name] 1-Feb-17 9:48am    
I just did.
Member 12977712 1-Feb-17 9:50am    
How do I use existing instance ?
[no name] 1-Feb-17 9:53am    
How can you ask that? Use the already existing instance that you have already created somewhere instead of creating a new instance.

1 solution

The problem is that you need to stop and think what you are doing.
frmPrincipal frm = new frmPrincipal();
frm.Show();
Will always create a new instance of a form and display it - it doesn't, can't, and very definitely shouldn't communicate with an existing instance any more than creating a new integer variable should "join" it to the previous one and affect them both when you set a new value!
And even if it did, you shouldn't be trying to access controls on that form because you shouldn't know they even exist! They are private by default for a very good reason - and making them public so you can do things like that is a very bad practice as it "locks" the two forms together, so you can't change one without considering the effects on all the other forms that might depend on it's internals.

What you want to do is "talk back" to the previous instance - which isn't difficult: Transferring information between two forms, Part 2: Child to Parent[^] will show you how to do it - and stop hiding forms when you are finished with them! Close them instead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900