Between two forms...

CuriousGuy

Member
Joined
Aug 8, 2022
Messages
7
Programming Experience
1-3
Hi everybody,

I have two forms
form1 and form2

On form1 i got 1 textbox and one button.
When i click on that button it open me form2 and there is a DataGridView with some data from my MSSQL DB.
When i do an double click on one row, it should pass the data from the selected row to to the textbox on my form1.
I did it and it works.
But there i got a problem...
It open the form1 twice, what i don't want. I want, that it pass the data to the current open form1.
(I did attack two screenshots)

My codes;
Form1:
C#:
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frx = new Form2();
            frx.ShowDialog();
        }
Form2:
C#:
private void Form2_Load(object sender, EventArgs e)
        {
            this.mYRECORDSTableAdapter.Fill(this.okulDataSet1.MyRecords);
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Form1 fr = new Form1();
            fr.textBox1.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
            fr.textBox2.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
            fr.ShowDialog();
            this.Close();
        }
It's maybe a very noobish question but i really stucked on this point. Thank you in advance.
 

Attachments

  • 01.jpg
    01.jpg
    61 KB · Views: 23
  • 02.jpg
    02.jpg
    73.9 KB · Views: 18
Last edited by a moderator:
The second form doesn't need to know about the first form, instead you add a property or two to second form that returns the value(s) you want. When dialog returns in first form it can retrieve those values.
 
Hi everybody,

I have two forms
form1 and form2

On form1 i got 1 textbox and one button.
When i click on that button it open me form2 and there is a DataGridView with some data from my MSSQL DB.
When i do an double click on one row, it should pass the data from the selected row to to the textbox on my form1.
I did it and it works.
But there i got a problem...
It open the form1 twice, what i don't want. I want, that it pass the data to the current open form1.
(I did attack two screenshots)

My codes;
Form1:

private void button1_Click(object sender, EventArgs e)
{
Form2 frx = new Form2();
frx.ShowDialog();
}

Form2:

private void Form2_Load(object sender, EventArgs e)
{
this.mYRECORDSTableAdapter.Fill(this.okulDataSet1.MyRecords);
}

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
Form1 fr = new Form1();
fr.textBox1.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
fr.textBox2.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
fr.ShowDialog();
this.Close();
}

It's maybe a very noobish question but i really stucked on this point. Thank you in advance.

Ok i found out how to do it, sorry for disturbing forum. Thread can be closed :)


Form1 :
C#:
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frx = new Form2();
            //frx.ShowDialog();
           --> frx.ShowDialog(this);
        }
Form2 :

C#:
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //Form1 fr = new Form1();
           --> Form1 fr = (Form1)this.Owner;
            fr.textBox1.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
            fr.textBox2.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
            //fr.ShowDialog();
            this.Close();
        }
 
Last edited by a moderator:
Or use the Application.OpenForms collection to find the existing open Form1`
While it can be avoided for example by checking the form Name as string and getting stuff from its Controls collection, using OpenForms often means a type test and/or cast to better interact with the right instance, which then again introduce the two-way coupling/dependency.
 
And furthermore, makes a unit testing even more difficult. To test one the second form, you need to have the first form up and running. You'll be closer to an integration test rather than a unit test. Essentially, the Application.OpenForms now becomes a facade that hides a global. Unit testing with globals and singletons is hard. Reasoning with globals and singletons is hard when you start having multiple threads, and/or opportunities for re-entrant code.
 
Back
Top Bottom