3 ways to open WinForm

xiaoxiao2021-04-01  271

In developing practice, we sometimes limit the operational behavior of certain forms, such as letting users only open a form. For this behavior, we can't say to the user. "This menu item can only click once, and you can't click on it after the window is open." As a responsible programmer, you will find ways to solve this problem.

On this issue, I have come up with two ways before and after. The first way is actually solved in the same way; the second approach will achieve a simple interface.

Method 1:

Create a shared variable, save the current reference to the inside, example code:

Public class myform inherits system.windows.forms.form ........ 'Statement as a member variable for the form MREFPUBLIC Shared MREF as System.Windows.Forms.form

'In the form of the LOAD event, save the reference of the form instance into the MREF = me' in the form's closed event, set the MREF to NothingMREF = Nothing .........

Before opening this form, do some judgment: DIM frM as myformif isnothing (myform.mref) THENFRM = new myform frm.show () frm.bringtofront () Else frm.bringtofront () End () End () This can guarantee this The form can only be opened once. Here, the key role is a member variable of the Shared type. It is the same as the life cycle of the application. If it is declared as a Private or protected, it can only be accessed by the member of the class, if it put it The declaration is public, and its role is equivalent to global variables. In the above code, we are cleared when the form is loaded when the form is loaded into the MREF, and it is cleared when the form is turned off, so naturally. We can judge the form by MREF before the instance of this form is created. Whether it has been opened.

This method is simple, and it is also very practical, in a few lines of code, but in real-world development, put these lines of code in each form, and the complex business logic processing code in the form Together, I think of something uncomfortable in my heart, and more lines of code will have a little error, so when I encountered the same problem in my second project, I think I have to encapsulate this function. Can be called directly.

Method 2:

I first thought of using a function to create a form, and after this form is created, save it to a static list. Public module fm1 public function code () As Object static reflist as arraylist if isnothing (reflist) Then Reflist = new arraylist .......... End funnd mode, but this idea is quickly proven to be wrong, 1 : Don't know the type of form instance to be created; 2: How to reference the instance saved in Reflist when the created form is turned off, how to save the Reflist.

Later I thought, only let the form created once, is the concept of global management of the form, then do you have a global variable to manage the form? This will undoubtedly bring trouble because this will introduce new global variables in order to use this component. At this time, I thought of Shared type variables again. I only need to build a global variable for this form, and declare this variable as private shared to ensure that each form can access it, and Ensure this feature itself package. My second way is to create a form base class, encapsulate the function into this form, and the form that needs to be used inheritably. First consider using the private shared type member variable to save the already opened form instance reference, and then consider whether the reference to which the form instance already exists, if there is, the newly created instance is logged out, return The window instance that has been opened. Specific code: public class frmopenonne inherits system.windows.forms.formprivate shared mreflist as arraylist = new arraylist

'Creating Forms Public Shared Function Create (FRM AS frMopenon "AS frMopenonce if isnothing (frm) THROW New Exception (" Form is not instantiated ") DIM ITEM ASFRMOENOPENCE for Each Item In MREFLIST ITEM. gettype.tostring = frm.gettype.tostring then frm.dispose () return item end if End For 'instance reference Add item = frm mRefList.Add (item) return itemEnd FunctionProtected Overrides Sub OnClosed (ByVal e As System.EventArgs) Dim item AS frMopenonce Dim POS AS INT32 = -1

For Each Item in MREFLIST POS = 1 if me.gettype.tostring = item.gettype.tostring the 'discovers the reference exit for end if next' of the same form deletes the object if Pos> -1 Ten Mreflist.removeat POS) End Sub Now, by inheriting the form, it is possible to ensure that a form is only opened once, for example: public class myform inherits frmopenonce ............... ... END SUB Create the form of this form: DIM frM as myform = myform.create (new myform) Whether it is performed in the above code, it will only open a form.

Measures 3:

Above the code has a place to make me more and more unhappy, it is necessary to create an instance of this form before judging. Is there a way to judge directly through the form type? I remembered that when I was programmed, I used activator to create an instance of the remote object, I think I should also use this class to create an instance of a local object. I saw the help information about Activator in the .NET SDK, I found that the .NET framework is really powerful. Let me contribute my final version: public class frmopenonne inherits system.windows.forms.formprivate shared mreflist as arraylist = new arraylist

'Create through the name of the type, if the name is incorrect, then throw an exception' filename: file name, frmType: Object type, the object created must be the object of the inheritance class of the class PUBLIC SHARED FUNCTION CREATE (Byval FileName As String) , Byval FRMTYPENAME AS STRING) AS frMopenonce Dim item as frmopenonce

For each item in mreflist if item.gettype.tostring = frmtypename life 'Discover the same form Return Item End if Next

Item = DirectCast (Activator.createInstanceFrom (filename, fmtypename) .unwrap, frmopenon "mreflist.add (item)

Return Item

END FUNCTION

Protected Overrides Sub onclosed (Byval e as system.eventargs) DIM ITEM AS FRMOENONCE DIM POS AS INT32 = -1

For Each Item in MREFLIST POS = 1 if me.gettype.tostring = item.gettype.tostring the 'discovers the reference exit for end if next' of the same form deletes the object if Pos> -1 Ten Mreflist.removeat POS) End Sub End Class

The above method determines if the instance of the form has been created by the type name, so it is better than the previous method. What is the good way to other friends, please enlighten me!

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

New Post(0)