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

zhaozj2021-02-16  52

Henry Inscribe - VB.NET Windows Forms (2)

Han Rui (12/26/2002)

After the previous post, there is a reader to believe and leave a message, think it is too simple? In fact, Henry has some selfishness. I am going to continue to discuss the event response in DataGrid (such as double-click Cell's complex process), but must involve the content of the event handling method, just the structure of this article allows Henry to expand properly The goal is reached. Although this is an incompariable problem, it is not easy to write, after writing this article, it is found that the structure is unless otherwise, in order to ensure the respect of the original author, this article has to remove this The scope of translation, there is a link here, and can be used by the reader. And take this opportunity to express their gratitude to the help provided by the original.

Add control

If there is no control, your form is not really useful. The .NET Framework Basic Type Library provides a large number of controls for use. You can use Button, Checkbox, TextBox, Label ... If you are still for .NET Framework, you still need to study Object derived trees. Take the Button control as an example, its derived relationship is shown in Figure 1:

System.Object

System.MarshalByrefObject

System.componentmodel.component

System.windows.Forms.Control

System.Windows.Forms.ButtonBase

System.Windows.Forms.Button

Figure 1 BUTTON control derived tree

All controls are derived from the COTROL class, adding a control to the form, requiring the following three steps:

(1) Define a control variable

(2) Instantian control

(3) Add controls to the form

Because VB.NET variables have different applications, where to declare a variable is the most important. The variables declared within a function can only survive within the function. If you want your control available throughout the class, define it at the class level.

Instantiate a control is like instantian other objects, use the New keyword. Once you have an instantiated control, you can add it to the form. Because the Form class does not have a method such as Add (Control), you cannot directly add the control to the form. However, the FORM class inherits the Controls property from the Control class. In the Form class, you can use this property to get the form's System.Windows.Forms.Form.ControlCollection object. This System.Windows.Forms.Form.ControlColLlection class represents a collection of controls included in the form. You can use its Add method to add a control into the form, remove a control from the form with a Remove method. To understand these three steps to add a Button control as an example, an example is as follows:

'Declaring Button Variables

Dim button1 as system.windows.forms.button

'Instantiate Button

Button1 = new system.windows.Forms.Button ()

'Add Button to the form

Me.Controls.add (Button1)

If you are not only adding a control, but to join multiple controls, do not use the add method to get an addition, but use the AddRange method to add a set of Control objects. For example, add a button to the form (ID: Button1), add a TextBox (TextBox1), you can write: me.controls.addrange (new system.windows.forms.control () {me.textbox1, me .Button1})

You can get the control you need by setting up multiple properties of the control. These attributes can be set before or after it is added to the form, but must be an instantiated control. The properties of a control typically have left, TOP, Width, and Height that controls their size and position. A Button property also has a Text property to display the text of the Button object, and the like. The following code is to continue the content of the previous section, supplement the initialized code:

Private button1 as system.windows.forms.button

Private textbox1 as system.windows.Forms.TextBox

private subinitializecomponent ()

Me.Button1 = new system.windows.Forms.Button ()

Me.TextBox1 = new system.windows.Forms.TextBox ()

Me.suspendlayout ()

'Button1

Me.button1.location = new system.drawing.point (200, 72)

Me.Button1.name = "button1"

Me.button1.size = new system.drawing.size (96, 56)

Me.Button1.tabindex = 0

Me.Button1.text = "button1"

'TextBox1

Me.TextBox1.Location = new system.drawing.point (216, 168)

Me.TextBox1.name = "textbox1"

Me.TextBox1.size = new system.drawing.size (64, 21)

Me.TextBox1.tabindex = 1

Me.TextBox1.text = "TextBox1"

'Form1

Me.autoscalebasesize = new system.drawing.size (6, 14)

Me.ClientSize = new system.drawing.size (392, 266)

Me.Controls.addrange (new system.windows.forms.control () {me.textbox1, me.button1})

Me.Name = "Form1"

Me.Text = "Henry's form"

Me.ResumeLayout (false)

End Sub

Here Henry wants to explain a few questions that are generally not paying attention to:

(1) Layout event: Add or remove the control in the form, or add or remove sub-controls in the control, the boundary changes of the form / control, and when other layouts that may affect the form / controls will A Layout event will occur. Therefore, when setting the initialization code, it should be prohibited (hang) this event, and then recover after the layout is completed. Otherwise, each addition is added, set a control will call a Layout event, re-layout the form, affect the work efficiency. So use me.suspendlayout () in the above example, and after the setting is completed, use me.ResumeLayout (false) to restore the event. (2) The habit of VB.NET is to use a collection, array to handle the previous individual behavior, we can see from the above example. When setting the position of the control, there is no attributes such as Left, Right, TOP to locate, but use the Location property to set the coordinates of the upper left corner of the control. Setting the control size, no properties such as Height, Width, but use size. This also shows a change idea of ​​VB.NET design and use, using a collection, can improve the simpleness of the code, and facilitate reading and management.

(3) The private declaration of Button1 and TextBox1 is explained in the following explanation.

Event processing

Previous versions of VB are event-driven, which you can easily write program code for response events. For example, you want to let the Button1 respond to the user's click (Click) event, all the things you want to do is to create an event program named Button1_Click (in fact, the VB integration compilation environment helps you have completed this). Event processing methods in VB.NET, and before. When you are using the control in the design window, you can click on it to inform Visual Studio.net You want to write a segment handler. Next you will see how manually write this code, and thus makes you understand how VB.NET's event works.

For Windows Event Drivers, youdomolically involved in three elements: objects, events, event handler.

So what is the above elements for us?

Object: Button1 Event: Click Event Handler: Button1_click

The language describes the language is: Click on button1 (object), will inspire the Click1_Click (Event Processor).

If you want an object to be able to respond to the event, you must use the WitHevent key when declaring it (the WitHevent Keyword indicated the declared object variable reference) can raise the class instance of the event). For example, when you want the button (Button) to respond to the event:

Private button1 as budton

Replace with:

Private WitHevents Button1 As Button 'This statement will change in its future, please pay attention to the reader

Add WitHevent before the variable name.

You will then need to create an event handler for each event to enable objects to call when responding to the event. Button1 To respond to the Click event, you need to have a Button1_Click process, and use the Handles to identify the control. Event.

Private sub button1_click (byval sender as object, _

Byval e as system.eventargs) Handles Button1.click 'writies yourself here

End Sub

The FORM class and all control classes have inherited the event from the Control class. If you look at the System.Windows.Forms.Control class in the .NET Framework reference, you will see 58 events, including Disposed events from Component inherited (for the inheritance of Disposed, see Henry Another document). There are also many new events such as Click, DoubleClick, GotFocus, Lostfocus, Keyup, KeyDown.

Many newcomers have such problems: What do you mean in the parameter sender and e in the event handler?

(1) Sender As Object, the source and use of it is broken. Object is all classes that support the .NET Framework class hierarchy and provide low-level services for derived classes. This is the final super-class of all classes in the .NET frame; it is the root of the type hierarchy. In general, Sender represents the source of the incident in the formation, ie "object" in the three elements mentioned above. If you write code in the event that is triggered (like this), you generally don't need to reassign it because it has default that it is the control. When you write a code to call an event program, you have to indicate what Sender is. (2) E, indicating that event data is an event that inspires the status (parameter) information required. Events that do not pass status information to event handlers when the event is initiated, and E is Eventargs. If the event handler requires status information, the application must be born from this class to save the data. For example, a mousedown event, the system needs to judge the location of the mouse, and it is determined which key click on the left. It is judged to click a few times, so the E will become an instance of the System.Windows.Forms.MouseEventArgs class.

When the event handler of several controls is the same, the "object name. Event Name" can be added to Handles, the same event handler can be called when the event is triggered. If the handler is different, you can identify through Sender, the code is as follows:

Private sub button1_click (Byval Sender As System.Object, Byval E as System.EventArgs)

Handles Button1.click, Button2.click

Select Case Sender.name

Case "Button1"

Msgbox ("You click Button1")

Case "Button2"

Msgbox ("You click Button2")

End SELECT

End Sub

Note: You will change the process name "Button1_Click" in the above example to "Henry", then run, is it the same? That is to say, you can write an event handler, and will not restrict multiple constraints in the fixed mode in VB. (But it is recommended that you use the name of "object name _ event name" to read)

The next section will introduce the problem of the inheritance of the form, please wait! I wish all new and old friends a happy new year! The career is successful!

----

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/ilatitude/

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

New Post(0)