VB.net four ways to prevent MDI child forms from being instantiated multiple times

Source: Internet
Author: User
Tags memory usage
Method One
may not be practical, just for simple projects:
Dim frmtmp as Classform "Implementation has designed a form called (class name) Classform
frmtmp = New Classform "Defines an instance of it here

Frmtmp.showdialog () is displayed as a modal form
This allows the menu buttons on the other form to be unavailable until the current form is closed, which avoids multiple instantiation problems.
Method Two
Applies only to situations where the form is not particularly numerous, and when there is no special requirement for multiple runtime memory usage
When the project is run, define each instance of the form that may be used, but do not display it, only when it needs to be displayed, X.show (x is the name of the form instance that you want to show), and use x.hide when you close it.
Method Three
Define the global variables for each form class that you want to display, and use the following code before you actually display the form:
If frm is nothing OrElse frm. Isdisposed Then
frm = New Form1
frm. Show ()
Else
frm. Activate ()
End If
Method Four
This method is especially useful for multiple document interfaces:
Dim Frmtmp as Form
"Search for each MDI child form will not be too many subforms open, so there is no speed problem
For each frmtmp in Me.mdichildren
"If find the Classfrm form you want to display
If TypeOf frmtmp is classfrm Then
Frmtmp.activate ()
Exit Sub
End If
Next
"If the form you want to display is not in the MDI child form
The new instance is defined and displayed.
Frmtmp = New ClassFrm400
Frmtmp.mdiparent = Me
Frmtmp.show ()

In the project, because of the TreeView effect, making the traditional method impossible to implement the subform at the top, for a long time, and finally used. ShowDialog () to achieve.



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.