VB.NET prevents four ways to instantiate multiple times by MDI child form

xiaoxiao2021-03-06  42

Since .NET handles each design Form as a class as a class, I believe many brothers and sisters who are used to VB6.0, I will really be uncomfortable for a while, and there may be inexplicable problems, this is here. " The second article developed in the NET environment, how to prevent the form from being instantiated multiple times. Problem Discovery: A menu item displays a non-molded form, clicking twice, and will display two entities at the same time. This article only lists the four ways, only for your reference.

method one

:

There may be no practicality, only simple items:

DIM FRMTMP AS CLASSFORM 'has implemented a form called (class name) ClassForm

FRMTMP = New classform 'defines one instance here

FRMTMP.SHOWDIALOG () 'is displayed as a mode form

This is not available before the current form is not closed, and the menu buttons on other FORMs are unavailable, and ingenious use can avoid multiple instantiation issues.

Method Two

:

Only for Form does not have a much more, and there is no special requirements for memory when running.

At the time of the project, define each instance of the FORM that may be used, but does not display, only when the call is required, X.Show (X is the FORM instance name you want to display), use X .hide.

Method three

:

The global variables of each required FORM class need to be displayed, and use the following code before the actual display of the form:

IF fm is nothing orelse frm.isdisposed then

FRM = New Form1

frm.show ()

Else

frm.activate ()

END IF

Method four

:

This method is especially suitable for multi-document interface:

DIM FRMTMP AS FORM

'Search for each MDI child form generally opened a child form, so there is no speed problem

For Each FRMTMP in Me.mdichildren

'IF finds the ClassFRM form you want to display

IF TypeOf frmtmp is classfrm then

frmtmp.activate ()

EXIT SUB

END IF

NEXT

'If you don't want the form you want to display in the MDI subform

'Define a new instance and display it.

FRMTMP = New ClassFRM400

FRMTMP.MDIPARENT = ME

FRMTMP.SHOW ()

Or above, you are welcome to make valuable opinions or other good methods

.

转载请注明原文地址:https://www.9cbs.com/read-78760.html

New Post(0)