Windows Forms Problem Closing From One To Another
-
I have a Main Form that calls various subforms with the following code on a button:
private void btnForm2\_Click(object sender, EventArgs e) { using (frmForm2 frmform2 = new frmForm2()) { Hide(); frmform2.ShowDialog(); Show(); } }
Then I have a single button on the other form(s) that has this code:
private void btnOK\_Click(object sender, EventArgs e) { this.Close(); }
Everything works great the first run through. I click the button on the main form and it goes away and I get the other form. I click ok on that form and go back to the original. Great! But then if I click to go back to the same form or any form off of the main again. Once that form comes up and I click OK again nothing comes back. As that form closes the original seems like it might briefly be displayed (I see maybe a quick outline of it) before going into never-neverland. The application itself never exits and Im still in debug mode then. I'm obviously doing something really stupid here... Any ideas?
-
I have a Main Form that calls various subforms with the following code on a button:
private void btnForm2\_Click(object sender, EventArgs e) { using (frmForm2 frmform2 = new frmForm2()) { Hide(); frmform2.ShowDialog(); Show(); } }
Then I have a single button on the other form(s) that has this code:
private void btnOK\_Click(object sender, EventArgs e) { this.Close(); }
Everything works great the first run through. I click the button on the main form and it goes away and I get the other form. I click ok on that form and go back to the original. Great! But then if I click to go back to the same form or any form off of the main again. Once that form comes up and I click OK again nothing comes back. As that form closes the original seems like it might briefly be displayed (I see maybe a quick outline of it) before going into never-neverland. The application itself never exits and Im still in debug mode then. I'm obviously doing something really stupid here... Any ideas?
-
I cant reproduce your problem. Please check if the MainForm goes behind any other windows that might be open (like Visual Studio).
--- "Drawing on my superior command of language I said nothing."
Well I did say it must be something stupid. Ok yes I have 100 things opened and it was beneath them, but I suppose essentially the same question comes around. Why do I get one result the first time through (The Main Screen shows back on top) and the second run through it does something totally different (The Main Screen shows behind other Windows). Or I suppose, more importantly, how do I fix it?
-
Well I did say it must be something stupid. Ok yes I have 100 things opened and it was beneath them, but I suppose essentially the same question comes around. Why do I get one result the first time through (The Main Screen shows back on top) and the second run through it does something totally different (The Main Screen shows behind other Windows). Or I suppose, more importantly, how do I fix it?
-
use BringToFront property of your main form e.g.
Form2 frm = new Form2(); this.Hide(); frm.ShowDialog(); this.Show(); this.BringToFront();
Yeah that solves the problem, though I dropped all the "this." I just don't know why I've never had to do this in the past. Or why it works on the first run through and back but not the second. I guess I'll just chalk it up to some kind of quirkiness in the MS C# 2005 Express Ed. Debugger. I just "upgraded" to it from a full 2003 version yesterday :)