Henry Instructions - A Probe into FORM in VB.NET Winform (3)
Han Rui (2002.9.25)
Third, where is Forms going?
The first section is a problem with a form of a form, and the second section discusses the switch between two windows.
(In fact, it can also be replaced with a switched between multi-wow, which discusses a multi-form problem.
This is the query sticker sent by a netizen in VB.NET version, as follows:
VB6 can be used
DIM FRM AS FORM
For Each FRM in Forms
......
Next FRM
Come get a window collection. How do this feature to do in VB.NET?
First analyze the meaning and role of Forms:
Forms is a Collection, which is an element that is loaded in the process.
Includes MDI Form, MDI CHild Form, and Non-MDI Form.
The Forms collection has only one attribute, which is count, indicating how many elements in the collection.
In order to meet the requirements, you should add your own Forms collection.
Add a new Module and write in Module:
Module Formscollection
Public Forms as new formscollectionclass () 'definition
End module
Add a class module:
Class FormscollectionClassClass
Implements IENUMERABLE
The 'Implements keyword is used to define a particular interface for class members.
'To use the for Each function, you must implement the IEnumerable interface, indicating the number of public enumerations,
This enumeration supports simple iteration on a collection.
Private c as new color ()
The 'Collection object provides a convenient way to reference a set of related items as an object. Item in the collection
(Or members) only need to exist in the collection. Members in the collection do not need to have the same data type.
It has two ways: adding an element with add, and removes elements from the collection with Remove. It has two properties:
Use count to record the number of elements, return to the location or key of the specific member of the Collection object with read-only properties Item
Sub Add (ByVal F AS Form) 'Method for adding elements to the collection
DIM Itemcount As INTEGER
For itemcount = 1 to c.count
'Judging whether f is in Forms, if it is not added
IF f.name = ctype (c.item (itemcount), form) .Name Then
EXIT SUB
END IF
NEXT
C.Add (f)
End Sub
Sub Remove (ByVal F AS Form) Method for Remove Elements to Collasses
DIM Itemcount As INTEGER
For itemcount = 1 to c.count
'Judging whether f is in Forms, if you delete it?
IF f.name = ctype (c.item (itemcount), form) .Name Then
C.Remove (itemcount)
EXIT for
END IF
NEXT
End Sub
Readonly Property Item (Byval Index) AS Form 'Returns the content of a location in a collection
Get
Return C.Item (INDEX)
END GET
End Property
Readonly property count () AS integer 'records in the number of elements in the collection
Get
Return c.count () End Get
End Property
OVERRIDABLE FUNCTION GETENUMERATOR () AS _
IEnumerator Implements IENUMERABLE.GETENUMERATOR
'Returns the number of enumerations that can be cyclically accessible. Once the Ienumerable interface is defined for the class, you must rewrite
GetENUMERATOR. When you call GetEnumerator, it will build an enumeration object in which a collection is included.
The current version number and reference to the collection item. Every time you access the enumerated number, it will put the enumerated version of the number and collection
The version is compared. If the version does not match, it is considered that the collection is changed, thereby abnormal.
Return C.GeteNumerator
END FUNCTION
END CLASS
Through this class, you can get your own collection of forms. Now Forms has
Characteristics of Collection and Enumeration. That is, increasing or deleting corresponding FORM from Collection
In the new process of Form1 in the main program (folded by Region, see the first section discussed in this series),
Join the next sentence:
Forms.Add (me)
have to be aware of is:
This sentence should be added after INITIALIZEC MOMPONENT (). This is because we prevent repetition
A means of adding an FORM is to determine using the Form's NAME. Just as I am in the first quarter
As described, the system puts the initialized content, including Name is written in InitializeComponent ()
The child is in the process. Because every new instance, it will call it once, so you must in the Add method.
Join the contents of judgment repeatability.
In the Dispose method, join the next sentence:
Forms.Remove (ME)
Here, we can review the sectations of the sect in the first section, because Forms is globally,
So when an Form is closed, it does not quit the survival period, so this form should be closed.
Delete from the collection. Why do you call the Dispose method when you turn it off? Please look back in the first section.
If you have a lot of form in your program, you have to join the two sentences in each form of New.
You can use it next:
DIM FRM AS FORM
For Each FRM in Forms
IFfrm.name = "form2" THEN
frm.close ()
END IF
NEXT
The current Forms is more flexible than the collection method of the previous Count property, more. Of course, the loss is an automatic function.
When used in multi-form, please refer to the use of the second section, consider it.
---- Declaration: The right to copyright and interpretation of this article belongs to Han Rui, if you need to reprint, please keep your full content and this statement. QQ: 18349592