Henry Instructions - A Probe into FORM in VB.NET Winform (1)
Han Rui (2002.9.25)
Some netizens often send posts or write a letter directly on my hosted layout, why VB.NET and VB6 are so much worse. Of course, the most asking is about the form of the form, which is also a change in VB6, and the most object-oriented (limited to the space, please refer to the object-oriented book) . When a friend shouted vb.net, how did you do this ... (I cut out the words here), I have to persuade him to endure, and then go deep into a little, and after the happiness will let him understand the world.
Let's start our history today. For friends from VB, there is no friend who tests with Java, it may be slightly troublesome, please contact me if you have any questions.
First, why can't my Form Show come out?
Is it Microsoft busy? Forgot some of the needs of two form? Of course, it is impossible, IF is forgotten, that Microsoft is really weak.
Once I remember, how the FORM2 is called in Form2 in the VB:
Form2.show (no discussion mode problem)
However, after writing FORM2 in VB.NET, there is no such thing as we want to think in the properties box!
Cause: Visual Basic .NET cancels the "default form instance" in earlier versions, and specifies that the properties, methods, and controls of the form can only be accessed by reference form instances.
It turned out to be like this, and the form in VB.NET also turned into a class! Let's analyze the generation code of a form. Please build a Form: Form3 with VB.NET. Then look at the code and a sentence: the purple comment is me, green is the system comes with.
Definition method for public class form3 'class
Inherits System.Windows.Forms.Form 'tells us where this is inheritance
#REGION "The code" Code "#Region instruction is used to fold and hide the code section in the Visual Basic .NET file. Through this method, you can better plan our code.
Public Sub New () 'Construction
MyBase.new () 'is used to explicitly call the base class constructor from the derived class constructor. This will also explain this.
'This call is required for the Windows Form Designer.
InitializeComponent () 'If your initialization code has a lot of lines, you can create a child process to manage these code, the name of the child is initializeComponent (). See later.
'Add any initialization after INITIALIZECOMPONENT ()
End Sub
'Form rewriting disposal to clean the component list.
The 'OVERLOADS key declares attributes or methods with the same name as existing members, but the parameter list is different from the original member. Overrides Keyword Specify attributes or methods to override members inherited from the base class. This problem is not very clear, please check the related content of the overload and inheritance of the book to the object. The author plans to write a "VB.NET inheritance and interface" after a few days, and the boundary will be described in more detail.
Protected Overloads Overrides Sub Dispose (Byval Disposing as Boolean)
Destructure of '
IF Disposing then
If not (Components Is Nothing) Then 'Clear the content in the component list
Components.dispose ()
END IF
End ifmybase.dispose (Disposing)
End Sub
'Windows form
Private Components as System.comPonentModel.icontainer
'System.ComponentModel Namespaces Provides classes for implementing component and control runtime and design-time behavior. The icontainer interface provides the function of the container. The container is an object that logically contains zero or more components. That is, by this definition, Components becomes a container that can contain all components of the current window.
'Note: The following process is necessary for the Windows Form Designer.
'You can modify this process using the Windows Form Designer.
'Don't modify it using the code editor.
'Use <> to indicate characteristics in VB.NET. Can complete some unique features provided by some systems.
'Form3 The following defines the properties of the initialization window.
Me.autoscalebasesize = new system.drawing.size (6, 14)
The value of the 'AutoScaleBaseSize property is used when the form is displayed, and the zoom factor of the form is used to calculate the form. The form uses the automatic zoom base size as a reference to compare the font size of the system to determine the amount of the form of the form when using the automatic zoom.
Me.ClientSize = new system.drawing.size (336, 309)
Me.Name = "Form3"
Me.Text = "Form3"
'When the user layout the control on the form, the initialization attribute of the control is also written here.
End Sub
#End region
END CLASS
Here to supplement the role and meaning of mybase keyword:
MyBase keyword behavior is similar to the object variable of the base class of the current instance of the category. MyBase is often used to access base classes that have been rewritten or hidden in derived classes.
In the above code, MyBase refers to the system.Windows.Forms.form class.
Thus we can know that myBase.new in Sub New is intended to inherit the initialization content of the System.Windows.Forms.Form class during the Form3 construct. The same method of MyBase.Dispose is destructured by the destructor of the base class System.Windows.Forms.form.
Summary, the current form is an authentic class, we should change your vb.net to the FORM in VB.NET. Forget the FORM usage in that VB, it is just a case of the System.Windows.Forms.Form class.
So, go back to the beginning of the problem, how to achieve FORM2 function in Form1 in VB.NET? Just give the class to the instance:
DIM FRM2 AS New Form2 () 'Defines an instance of the Form2 class
FRM2.SHOW () 'The method of calling the class by instance, the same problem is not discussed
Is it very simple, then I will pick up again. When we declare that FRM2 is used in New, it is natural to construct this FRM2 using Sub New in the Form2 class. So where are we calling Dispose to destructure New's FRM2?
The form.dispose method is to rewrite the Since the Control.Dispose method, how is the meaning of the control.dispose method? Its role is to release the non-hosting resources occupied by Control, and can also releasing the hosted resource. When the parameters are [Visual Basic]
Disposing is True releases managed resources and unmanaged resources; only unmanaged resources are released for False. FORM class Disposing is True. The function that automatically calls Dispose is to start the form of the .NET's public language runtime, run the library automatically handle object layout and managing references to objects, and release them when you use objects. Its survival is called managed data in this way. Automatic memory management eliminates memory leaks and other common programming errors. Of course, it is not universal, this is not the focus of this article, first pressing the table.
Any type of Dispose method should release all resources it owns. It should also release all the resources owned by the base type by calling its parent type DISPOSE method. The parent type DISPOSE method should release all resources it owns and also call the DISPOSE method of its parent type, thereby propagating this mode in the entire base type hierarchy. To ensure that resources are always properly cleaned, the Dispose method should be able to be used multiple secure calls without causing any exceptions.
When we turn off a window, a termination response will be issued and the window object (like the FRM2 defined above) is sent to the termination queue, the garbage collector of the public language running library tracks the survival of this object, at this time This object base class (such as Form2) is called, which is used to destroy the object and return resources.
If DISPOSE is not cleaned or not called Dispose, the system's garbage collector calls the target's Finalize method to clear. Since the implementation of the Finalize method is greatly damaged, we will not use it to make clear work at first.
Through this section, I hope that you have established a concept, and the Window in VB.NET has become a real class, but also provides a complete set of solutions, and cannot implement class inheritance in VB6. defect. In the next section, we will start discussions in multi-form programming.
----
Disclaimer: 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