Use C # To open a new window and close the old window; Use winform to prevent repeated opening of multiple identical subforms

Source: Internet
Author: User

1. the most widely used method on the Internet is the hidden method, the principle is to hide the old window and open a new window. the Code is as follows:
// write the code in a proper position in the old window
// create a newform window (newform is a self-defined form) newform fm = new newform ()
This. hide (); // hide the current window FM. show (); // new window display
in this way, the old window still occupies the memory, so if it is not good for login windows, however, if the old window is the main window to be restored later, this method is very good, because you can close the new window and use the old window later. show () to restore the old window. Therefore, this method is suitable for transferring information between the main window and the subwindow.
2. design the logon window. defined in program. CS as follows:
Static void main () {application. enablevisualstyles (); application. setcompatibletextrenderingdefault (false );
// Create a login window (login is a self-defined form) login log = New Login ();
// Use the mode dialog box to display log. showdialog ();
// Dialogresult is used to determine whether to return the IF (log. dialogresult = dialogresult. OK) {// open the main form application in the thread. run (New Main ());}} In the login window, log. dialogresult = dialogresult. OK is assigned after verification. In this way, the logon window is closed and the main window is opened.
I still don't quite understand why it is so troublesome, but the general reason should be that "a main form must always exist, and all the forms created on it have its own, if you close it, the rest will naturally be closed ". I am also learning

 

Private bool showchildrenform (string p_childrenformtext) {int I; // checks the subforms of the current form in sequence for (I = 0; I <this. mdichil.pdf. length; I ++) {// determines whether the text attribute value of the current subform is the same as the passed string value if (this. mdichil1_[ I]. TEXT = p_childrenformtext) {// if the value is the same, this sub-form is the sub-form to be called. Activate this sub-Form and return the true value this. mdichil1_[ I]. activate (); Return true ;}/// if there is no same value, it indicates that the subform to be called has not been opened, and the return value is false ;} when using the MDI child form, if you only use from. show () code, then we click the menu several times Open several subforms. You can use this code to prevent this situation. First, add a function to check whether the specified subform has been opened. If yes, activate the subform, otherwise, false is returned and the following code is written in the method of calling the subform if (! Showchildrenform ("text attribute value of the subform") {form newform = new form (); newform. mdiparent = This; newform. show ();} this way, when calling the subform, it will determine whether the same instance of this form has been opened. If yes, the opened form will be activated, otherwise, a new instance will be opened.

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.