How can I make one Form show another?
-
-
Hello all, Here's my problem. I have a Windows Form with some controls on it. When the user clicks a button, I want a second form to appear, and the first one to be hidden. I've drawn up both forms, so how can I make Form1 display Form2? Thanks!
-
Form2 frm=new Form2();
frm.Show();
this.Hide(); //or this.Close();Anonymous? :(
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi -
Thanks. Sorry for posting Anon. I forgot my password and I'm on a coding spree, so don't want to hunt it down. :)
-
Ok. :)
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma GandhiI have one other detail I hope you won't mind answering. When the second form is closed via the close button (next to the minimize and maximize buttons in the upper right) the second form is disposed. However, Form1 remains, albeit hidden. So the program doesn't terminate. I've tried calling Form1.Dispose() after Form2 is shown but that just quits the program. Any thoughts on how I can make the program quit when the user clicks the X?
-
I have one other detail I hope you won't mind answering. When the second form is closed via the close button (next to the minimize and maximize buttons in the upper right) the second form is disposed. However, Form1 remains, albeit hidden. So the program doesn't terminate. I've tried calling Form1.Dispose() after Form2 is shown but that just quits the program. Any thoughts on how I can make the program quit when the user clicks the X?
You'll want to do this.Close(), not this.Hide. But that will release the ref to Form2, which may make it close, too. How about
Application.Run(new Form2());
this.Close();"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi -
You'll want to do this.Close(), not this.Hide. But that will release the ref to Form2, which may make it close, too. How about
Application.Run(new Form2());
this.Close();"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma GandhiI get this error: An unhandled exception of type 'System.InvalidOperationException' occurred in system.windows.forms.dll Additional information: It is invalid to start a second message loop on a single thread. Use Application.RunDialog or Form.ShowDialog instead. Trying Application.RunDialog gives me this error: C:\Documents and Settings\...\Form1.cs(402): 'System.Windows.Forms.Application.RunDialog(System.Windows.Forms.Form)' is inaccessible due to its protection level Not sure exactly what that means, but any other thoughts? (Oh, and this.Close() on Form1 does quit the app.)
-
I get this error: An unhandled exception of type 'System.InvalidOperationException' occurred in system.windows.forms.dll Additional information: It is invalid to start a second message loop on a single thread. Use Application.RunDialog or Form.ShowDialog instead. Trying Application.RunDialog gives me this error: C:\Documents and Settings\...\Form1.cs(402): 'System.Windows.Forms.Application.RunDialog(System.Windows.Forms.Form)' is inaccessible due to its protection level Not sure exactly what that means, but any other thoughts? (Oh, and this.Close() on Form1 does quit the app.)
I went into the Closed event for Form2 and just put in this line, which works: Application.Exit(); Still, I'd like to know if there's a way to do it without having to add that. PS: Since I'm posting so much, I thought I should hunt down my p/w and login! ;)
-
I went into the Closed event for Form2 and just put in this line, which works: Application.Exit(); Still, I'd like to know if there's a way to do it without having to add that. PS: Since I'm posting so much, I thought I should hunt down my p/w and login! ;)
That's the way I was going to suggest doing it. :) frogb0x wrote: Since I'm posting so much, I thought I should hunt down my p/w and login! :-D
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi -
That's the way I was going to suggest doing it. :) frogb0x wrote: Since I'm posting so much, I thought I should hunt down my p/w and login! :-D
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi