Henry Inscription: VB.NET Windows Form Introduction (1)

zhaozj2021-02-16  56

Henry Inscribe - VB.NET Windows Forms Introduction (1)

Han Rui (12/21/2002)

-Merry x'mas! Every friend who has been supporting my, I hope that you can feel the warmth of the fire in every winter, encouraging me to write a little thing, and I hope that you can get it in communication, get it progress.

[Abstract] In VB.NET, the Windows Form has a large change. A Windows form now represents an instance of the System.Windows.Forms.form class in the .NET architecture. This article provides an extraction of new technologies through multiple routines such as how to write a basic task such as a Windows application. This includes: Creating a form, add control, event response, etc. This article also provides object-oriented analysis of WinForm, including the most powerful new features - code can be reused: inheritance.

Note: The content of the Beta version is used in the original routine. In the translation, it has been changed to the official version, please pay attention to the author of the original text. And on the basis of the original text, the translator deleted the content of some OOP basic theories, joined many new content, so that this article is more fulfilling, in this sense, this article is not a faithful translation. I hope the original author will forgive me.

The new version of VB adds a new feature: inherit to make it referred to as object-oriented programming languages. Now, no one will complain that VB is not a pure OOP (Object-Oriented Programming Object-Oriented) language. But what does this mean for VB programmers throughout the world? Is it meant that every VB programmer must now learn OOP? Yes, but the answer will not be so simple. It is well known that ease of use is a major feature of the previous version of VB. Everyone will remember that the drag and drop control and the creation of a Windows application are so convenient and do not need to understand the OOP. In those versions, most of the programmers will often use its semi-hanging OOP feature. Easy to use the characteristics of the Visual Studio .NET, create a VB Windows application is still as easy as the past. However, this article will show you why you have to master OOP to truly use VB.NET to be completely powerful.

Create a VB.NET form

VB.NET programmers can use the .NET Framework basic type library to quickly create a Windows application. System.Windows.Forms Namespace is provided to you to create a Windows Form. Create a form, you simply create a new class that is derived from the System.Windows.Forms.Form class. The following example creates a new class named Form1 from System.Windows.Forms.Form

Public Class Form1: inherits system.windows.forms.form

END CLASS

Note: If you write the inherits declaration part in another, you will not need the colon between the separated class name and the inherits keyword.

Public Class Form1

Inherits System.Windows.Forms.form

END CLASS

Because when you work with a form, you will use classes from the System.Windows.forms namespace, you can introduce (import) namespaces to avoid writing namespace names in front of you when you use each class.

Imports System.Windows.Forms

Public Class Form1

Inherits Form 'Look at this difference between this sentence and the above example

END CLASS

If you use Visual Studio.net, it will write some initialization code. If you don't know who is who is, it will be confused. After you create a new VB.NET application, enter the code editing window of the first form, remove all the contents, and type the code described above. Press F5 to run the app, you will see a blank form (shown in Figure 1). Figure 1: Blank form

Very simple, is it? How did this do it? Because our Form1 class is derived from the System.Windows.Forms.form class, the Form class contains the elements required to implement the form. The Form class is the seventh level of the entire derived tree, which is derived from ContainerControl, while ContainerControl is also derived from ScrollableControl, and so on.

Please see Figure 2 below:

System.Object

System.MarshalByrefObject

System.componentmodel.component

System.windows.Forms.Control

System.windows.Forms.ScrollableControl

System.Windows.Forms.ContainerControl

System.Windows.Forms.form

Figure 2: System.Windows.Forms.Form class derived tree

You may still have a doubt that you may display a form of a form? In fact, Visual Studio.net has helped you have made a lot of things. First, in the VBProj file, it specifies the class that starts running. This is like a hidden Sub Main. By default, the first form (such as the FORM1 in our example) will be the first class that is created. Also, when you don't have to construct a function to create a class, the VB.NET compiler sets a non-developing constructor:

Public Sub New ()

End Sub

Therefore, our FORM1 class actually has a constructor. And explicitly created this constructor is not a bad idea.

Public Class Form1

Inherits System.Windows.Forms.form

Public Sub New ()

'Write your own code

End Sub

END CLASS

How does this constructor implement inheritance of the base class FORM class? The compiler actually added you as follows:

Mybase.new ()

MyBase keyword behavior is similar to the object variable of the base class of the current instance of the category (equivalent to this this). MyBase is often used to access base classes that have been rewritten or hidden in derived classes. Specifically, mybase.new is used to explicitly call the base class constructor from the derived class constructor. Therefore, write the above code again, that is:

Public Class Form1

Inherits System.Windows.Forms.form

Public Sub New ()

Mybase.new ()

End Sub

END CLASS

Write your own code

When you instantiate an object with the new keyword, a class constructor will be called. In the constructor, you usually write some initialization code. If you have a lot of code for initialization, you can create a private process (SUB) to handle initialization work, ensuring your code readability. For example, by default, Visual Sudio.net creates a process called InIitIzeComponent and calls it in the constructor.

Use this to the previous code, we can get: public class form1

Inherits System.Windows.Forms.form

Public Sub New ()

Mybase.new ()

InitializeComponent

End Sub

Private subinitializeComponent ()

'Write initialization code here

End Sub

END CLASS

Keep in mind that you can use the keyword me to represent an existing class instance, you can write the title of the following record form and the size of the initializeComponent process:

Private subinitializeComponent ()

Me.Text = "Henry's form"

Me.width = 400

Me.height = 300

End Sub

When you press F5, you will be able to see a width of 400pixels (pixel), a form that is 300pixels. Although it is still a blank form, now its title has been changed to "Henry's form"

Figure 3 Width = 400, Height = 300, blank form with title

Due to the failure of initialization, the damage caused by the program is huge, so Visual Studio.net is pleased to remind you before the initializeComponent process:

'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.

Many people have been confused by the role of , is actually very simple, because the initialized code is modified by the form designer, so your program has problems, the initialization code does not have problems (if you press If you recommend, after you join this feature, you will skip the InitializeComponent process without debugging when you debug, this is called "debuggerstepthrogh". You can also learn such code in your own program to reduce the complexity of debugging.

The current code should be:

private subinitializecomponent ()

Me.Text = "Henry's form"

Me.width = 400

Me.height = 300

End Sub

Writing this, the reader will find that there is still a lot of content in the [Region "Windows Form Designer Generated"] in its own program, don't involve, don't worry, next time you look!

----

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

E-mail: Henry7685@hotmail.com

Please visit my column: http://www.9cbs.net/develop/author/netauthor/latitude/

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

New Post(0)