Visual C# Developer Center >
Windows Forms Forums
>
Windows Forms General
>
When showing Form2 from Form1, Hide() Hides both forms.
When showing Form2 from Form1, Hide() Hides both forms.
In my main form (Form1) I use a timer control, and every 10 seconds it instantiates another form (Form2) if needed.
It works great, but when I minimize Form1, the app will also hide Form2, when I actually want Form2 to
be visible.
How can I get Form1 to hide, but not Form2?- Moved byTaylorMichaelLMVPThursday, November 05, 2009 6:48 PMWinForms related (From:Visual C# General)
Answers
- Hi,
Make sure you aren't using ShowDialog or passing a reference of form1 to the show method of form2, in either of those cases form1 becomes a sort of 'parent' and if hidden its 'child' will also be hidden.- Marked As Answer byKira QianMSFT, ModeratorMonday, November 09, 2009 9:40 AM
- Put it in the constructor.
Form2 form2 = new Form2(form1); // Now form2 has a reference to form1
form2.Show(); // form1 isn't the parent
The better way to do it is to define some events in form2 so it doesn't have to know about form1, but that's a little more complicated.- Marked As Answer byKira QianMSFT, ModeratorMonday, November 09, 2009 9:40 AM
I agree with you - I'm brand new to OO programming, and C#. I'm trying to write an app that will use a timer and
display a 2nd form every so often, and if the user closes the 2nd box, the 1st form needs to know about it. So I wrote the 2 forms in a parent/child manner:
In this case I would just have form1 connect to form2's FormClosed event, something like this;
Form2 form2 = new Form2();
form2.FormClosed += new FormClosedEventHandler(this.form2_FormClosed);
private void form2_FormClosed(object sender, FormClosedEventArgs e)
{
//Disconnect from the event, as the form is closed and we no longer care.
Form2 form2 = (Form2)sender;
form2.FormClosed -= this.form2_FormClosed;
//Form2 was closed, so do whatever you need to do here.
}- Marked As Answer byKira QianMSFT, ModeratorMonday, November 09, 2009 9:41 AM
All Replies
- can you put your code here, so we can have a look.
If the post is helpful or answers your question, please mark it as such! - Hi,
Make sure you aren't using ShowDialog or passing a reference of form1 to the show method of form2, in either of those cases form1 becomes a sort of 'parent' and if hidden its 'child' will also be hidden.- Marked As Answer byKira QianMSFT, ModeratorMonday, November 09, 2009 9:40 AM
- Thank you - that's what I wasn't clear on! yes - I'm passing the reference of form1 to form2 because I need to
share data between the 2 forms. Any suggestions on how to get around that? - Put it in the constructor.
Form2 form2 = new Form2(form1); // Now form2 has a reference to form1
form2.Show(); // form1 isn't the parent
The better way to do it is to define some events in form2 so it doesn't have to know about form1, but that's a little more complicated.- Marked As Answer byKira QianMSFT, ModeratorMonday, November 09, 2009 9:40 AM
- I agree with you - I'm brand new to OO programming, and C#. I'm trying to write an app that will use a timer and
display a 2nd form every so often, and if the user closes the 2nd box, the 1st form needs to know about it. So I wrote the 2 forms in a parent/child manner:
FORM1:
Form2 frm2 = new Form2(this);
FORM2:
form1 f;
public delegate void EVH();
public event EVH MyEVH;
public Form2(Form1 fr1)
{
InitializeComponent();
f = new Form1();
f = fr1;
}
etc.
So I need to change this whole thing. Any thoughts? - Hi,
As per Scotty's post, it isn't necessary to pass a reference to form1 to the show method of form2 in order to share data between them. Pass the form reference either in the constructor (as Scotty shows) or via a property, that's easiest. However, depending on what you're sharing and in what direction (form1 -> form2 or form2 -> form1) it might be a better design to either;
1. Pass in the individual values you want from form1 into form2 instead of the form itself.
2. Raise events from form2 that form1 can capture and retrieve the data from form2 (using properties on Form2).
3. Add properties to Form2 for the data that form1 requires and just read those properties at some later point in your code in form1. I agree with you - I'm brand new to OO programming, and C#. I'm trying to write an app that will use a timer and
display a 2nd form every so often, and if the user closes the 2nd box, the 1st form needs to know about it. So I wrote the 2 forms in a parent/child manner:
In this case I would just have form1 connect to form2's FormClosed event, something like this;
Form2 form2 = new Form2();
form2.FormClosed += new FormClosedEventHandler(this.form2_FormClosed);
private void form2_FormClosed(object sender, FormClosedEventArgs e)
{
//Disconnect from the event, as the form is closed and we no longer care.
Form2 form2 = (Form2)sender;
form2.FormClosed -= this.form2_FormClosed;
//Form2 was closed, so do whatever you need to do here.
}- Marked As Answer byKira QianMSFT, ModeratorMonday, November 09, 2009 9:41 AM
- I hope to be as good as you guys some day! Thanks for everything!
- Hope to be better than that ;)


