Two forms in C # will jump to each other

Source: Internet
Author: User

Turn from: http://blog.csdn.net/allenjy123/article/details/7727500

First of all, Program.cs

[CSharp]  View Plain  copy [stathread]   static void main ()    {        application.enablevisualstyles ();        Application.setcompatibletextrenderingdefault (False);          //  The automatically generated code is like this        // application.run (New form1 ());           //  Express   Instantiate a new  Form1  and show   at this time the program goes into message loop         //  once the  Form1  is closed, the program closes.        //  in order to get the program  Form1  can continue operation after shutdown   need to be modified under           new form1 (). Show ();       application.run ();          / /  do this to avoid  Form1  shutdown after the program automatically exits           //  But what is the risk of doing so?        //  Once the user forgets the  application.exit ();       //  The program is closed after all Windows   Its process still has no end        //  so  application.exit ();  This line of code is needed manually added    }  
Form1:button_click[CSharp]View plain copy Form2 f = new Form2 ();   F.show (); This. Close ();
Form2:button_click

[CSharp] view plain copy Form1 f = new Form1 ();   F.show (); This. Close ();
Note: Call Application.exit () when the last window is closed; Otherwise, the program process is not going to end.


Workaround: (assuming Form2 is the last window)

[CSharp]   View plain  copy Private void panel1_click (Object sender,  eventargs e)    {       Form2 f2 = new  Form2 ();       f2. Closed += new eventhandler (this.f2_closed);       //f2. closed +=  (Obj, args)  => { application.exit (); };        f2. Show ();       this. Close ();  }      private void f2_closed (object sender,  eventargs e)    {       application.exit ();  }   The method you have actually used

In C # WinForm, 2 forms, Form1 and Form2, perform this when switching to each other
. Hide ();
Form2 Form2 = new Form2 ();
Form2. Show (); At this point you will find that when the Form2 appears, the Form1 is hidden, but when you close Form2, the process is not closed. That means the resource is not released. I looked up a lot of information on the Internet, many did not solve, through my experiment I found:
as long as the main window is closed, all the resources will be released, but when the window is closed (FORM2), the resources are not released, at this time can be in the Form2_formclosed (object sender, Formclosedeventargs e) Add the following code to the event: Application.exit ();//notifies all messages to terminate, closes all forms after termination, and frees resources. The problem is solved.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.