Eric H
Eric H

Reputation: 1098

Close a windows form without exiting the entire application

Environment


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


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
  }

Upvotes: 3

Views: 5626

Answers (4)

Eric H
Eric H

Reputation: 1098

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.

Upvotes: 1

jfs
jfs

Reputation: 16798

Is it required to have 3 forms?

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

Upvotes: 0

Rich
Rich

Reputation: 3720

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.

Upvotes: 3

David Heffernan
David Heffernan

Reputation: 613441

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.

Upvotes: 3

Related Questions