Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
596 views
in Technique[技术] by (71.8m points)

c# - Close a windows form without exiting the entire application

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
  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...