Close a windows form without exiting the entire application
Asked Answered
C

4

3

Environment

  • Windows XP SP3 x32
  • Visual Studio 2005 Standard
  • Device/Platform: Honeywell Dolphin 9500 with Windows Mobile/Pocket PC 2003
  • NET Framework 1.1 and .NET Compact Framework Framework 1.0 SP3

Goal

I currently have an application with 3 forms. The first form is something like a splash screen but I have not yet decided whether or not the user will be allowed to reopen it. The second form is an aggregate listing of items that will be displayed, one by one, in the third form.

I would like to be able to open the first form and wait for a button press. When that button is pressed, I would like to open another form and dispose of the first. Once an item is selected out of the list box on the second screen, I would like to display the third form and possibly dispose of the second form. The user also needs to be able to reopen the second form to select another item to be displayed on the third form. That being said, I probably don't want to dispose of the second form. However, memory is an issue on this device (64MB shared between storage and system memory) hence my desire to dispose of things when I can.


Problem

You can probably guess this by the title, but when I close/dispose of my first form, the entire application closes. Now that I have read up on the matter a little, I am aware that this has to do with this line: Application.Run(new Form1()); or whatever my form happens to be named.


Things I Have Tried

  • this.Dispose() - closes the entire application
  • this.Close() - closes the entire application
  • I also saw multiple people recommending one instantiate their form (Form f1 = new MyForm();), show it (.Show();), and then use Application.Run(); with no arguments. When I try this, I get "No overload for method 'Run' takes '0' arguments"
  • ApplicationContext does not exist in version 1.1 of the .NET Framework

Code

static void Main()
  {
   Application.Run(new Welcome());
  }

  private void btnBegin_Click(object sender, EventArgs e)
  {
   Form wof = new WorkOrderForm();
   wof.Show();
   wof.BringToFront();

   // Here is where I would like to dispose of the Welcome form
  }
Cotterell answered 8/4, 2011 at 18:10 Comment(0)
C
3

You can call Application.Run() with your "main" form, this will still allow the application to close properly when the form closes, but hide it (Visible=false) whilst you show the splash screen, or just show the splash screen on top of it.

Cesarean answered 8/4, 2011 at 18:15 Comment(5)
Would I be "showing" the other forms from the Main() function? i.e. friendpaste.com/6UUCSKfqkYFxvW3lAT8xbj Doing that just shows me a blank form. I think now is a good time for me to state that I am a very green programmer, specifically in this environment. Thanks for your help/feedback thus far.Cotterell
I'm not sat at my IDE at the moment, however you'll want to do Application.Run(mainform) after constructing it and hiding it. Application.Run starts off a message loop, so your code will sit there and not progress onto the splash screen. You can launch your splash screen from the mainform load event. Alternatively look at the application framework settings on the project properties page, I believe you can set up a splashscreen there.Cesarean
I think one thing I may be doing wrong is this: My Main method is in my "splash-screen" form because thats the Form I created first in this project. Should/could my Main() function reside in some "driver" class/form so as not to tie it to a form that may be opening/closing? Again, thanks for your continued assistance.Cotterell
As the main method is static it doesn't really matter where it is, although it is conventional to put it in a "Program.cs" fileCesarean
Awesome. Thanks. That's the part I was missing!Cotterell
S
3

Create a hidden form that you pass to Application.Run(). When you decide it's time for the app to go down, close that hidden form.

Sordello answered 8/4, 2011 at 18:13 Comment(3)
Hmm...I had not thought of that. I will try that before trying ApplicationContext objects! Thanks!Cotterell
I feel like this is a dumb question, but if I am not residing in the form that has the Main method in it, how will I access the hidden form to close it? Thanks again.Cotterell
Well, that's really up to you. In any case, what Rich suggests sounds like a decent approach too.Sordello
C
3

You can call Application.Run() with your "main" form, this will still allow the application to close properly when the form closes, but hide it (Visible=false) whilst you show the splash screen, or just show the splash screen on top of it.

Cesarean answered 8/4, 2011 at 18:15 Comment(5)
Would I be "showing" the other forms from the Main() function? i.e. friendpaste.com/6UUCSKfqkYFxvW3lAT8xbj Doing that just shows me a blank form. I think now is a good time for me to state that I am a very green programmer, specifically in this environment. Thanks for your help/feedback thus far.Cotterell
I'm not sat at my IDE at the moment, however you'll want to do Application.Run(mainform) after constructing it and hiding it. Application.Run starts off a message loop, so your code will sit there and not progress onto the splash screen. You can launch your splash screen from the mainform load event. Alternatively look at the application framework settings on the project properties page, I believe you can set up a splashscreen there.Cesarean
I think one thing I may be doing wrong is this: My Main method is in my "splash-screen" form because thats the Form I created first in this project. Should/could my Main() function reside in some "driver" class/form so as not to tie it to a form that may be opening/closing? Again, thanks for your continued assistance.Cotterell
As the main method is static it doesn't really matter where it is, although it is conventional to put it in a "Program.cs" fileCesarean
Awesome. Thanks. That's the part I was missing!Cotterell
C
1

I keep answering my own questions...

I posted this identical issue on the MSDN forums and was told to use the ApplicationContext object instead of a new Form object as a parameter in Application.Run. I am going to try that now. For now I will leave this unanswered.

EDIT: Well, I recant. Application context does not exist in the .NET Framework v1.1

EDIT2: Actually, it seems that it does (http://msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext(VS.71).aspx), however it does not appear to exist in the Compact Framework version 1.0 SP3.

Cotterell answered 8/4, 2011 at 18:15 Comment(0)
B
0

Is it required to have 3 forms?

One way is to create 3 panels in 1 form and just show the active panel.

Bennett answered 8/4, 2011 at 18:17 Comment(2)
Having 3 forms is a perfectly acceptable way to create an application.Theatheaceous
@Theatheaceous Yes I agree. Just suggesting another possible solution to his problem.Bennett

© 2022 - 2024 — McMap. All rights reserved.