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 hiding method. The principle is to hide the old window and open a new window.
The specific 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 method, the old window will still occupy the memory, so if it is not good for the login window, but 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 one later. show () to restore the old window.
Therefore, this method is suitable for transferring information between the main window and the subwindow.
2. the login window is designed.
Defined in program. CS as follows:
Static void main ()
{
Application. enablevisualstyles ();
Application. setcompatibletextrenderingdefault (false );
// Create a login window (login is a custom form)
Login log = New Login ();
// Use the mode dialog box to display logs
Log. showdialog ();
// Dialogresult is used to determine whether to return the parent form
If (log. dialogresult = dialogresult. OK)
{
// Open the main form in the thread
Application. 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;
// Check the subforms of the current form in sequence
For (I = 0; I {
// Determine whether the text attribute value of the current subform is the same as the input string value
If (this. mdichil1_[ I]. Text = p_childrenformtext)
{
// If the value is the same, the child form is the child form to be called. Activate the child form and return the true value.
This. mdichil.pdf [I]. Activate ();
Return true;
}
}
// If the value is not the same, it indicates that the child form to be called has not been opened and false is returned.
Return false;
}
When using the MDI subform, if you only use the from. Show () code, you can click several menus to open several similar 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 values of subforms "))
{
Form newform = new form ();
Newform. mdiparent = this;
Newform. Show ();
}
In this way, when a subform is called, The system checks whether the same instance of the form has been opened. If yes, the opened form is activated. Otherwise, a new instance is opened.

Related Article

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.