VB.net event and event handling

Source: Internet
Author: User

Sometimes, a user might think that Visual Studio's engineering is a sequence of sequential processes, but in most cases the program is event-driven (driven)---that is, the program is run depending on something such as clicking the keyboard, Click on the mouse and some user-defined events. Unlike a linear program, it is sometimes impossible to decide which event-driven specific process to run. Events can be a fundamental part of today's programming because they allow users to write programs that respond to a world outside the object, such as user input and other object input information.
1. The event
Events are used to inform the application that something important is happening. For example, when a user clicks on a control in a form, a click event is triggered and an event-handling procedure can be invoked. Events can also be associated with unrelated processes. For example, there is a process that needs to do many things, such as a series of operations, in which a user can compose a series of events into a separate thread. If the user decides to cancel an action, the application can issue a cancel event to prevent this sequence of actions from taking place.
2. Event Dispatcher
An object that can raise an event is an event dispatcher, such as a form, a control, and a user-defined object.
3. Declaration of events
In a class, the declaration of an event is performed with the keyword event. For example:
Event anevent (Byval eventnumber as Integer)
4. Initiation of events
An event is like a very important thing that happens, and the act of declaring this important thing is called an event initiation. From a practical standpoint, an event cannot be used as a method, just as a click event that cannot be button1.click to trigger a button. The event must be raised using the RaiseEvent keyword. The following example raises an event named Anevent:
RaiseEvent Anevent (Eventnumber)
5. Handling of events
Event processing is a process that is invoked after a related event occurs, and vb.net automatically creates an event handler if the user double-clicks a control on a form in the editing environment or selects an event from the Drop-down box in the Properties window. Visual Basic uses a standard convention representation to name the process, which is to underline the generator of the event (the object that raised the event) and the name of the event. For example, a click event named Button1 can be represented as this:
Sub Button1_Click
Users can name the events they define by using this nomenclature, but there is no need to do so if the user uses the Handles keyword.
6. Linking event and event handling
Before an event processing can be used, the user needs to associate it with an event with WithEvents or AddHandler.
WithEvents's Statement and Handles clause declare an event-handling method, regardless of the WithEvents variable, and the assigned object can inspire a particular event to be handled.
AddHandler and RemoveHandle statements can allow users to dynamically connect and disconnect event and event handling processes.
In some cases, such as linking events to some standard controls, VB.net will associate event handling with an event. For example, in VB.net's previous version of the design environment, the user could double-click a button control, and then an empty event-handling process arose, in which the user could add code, and behind the code, Visual Basic can associate event processing with events by implicitly generating a WithEvents variable, and this empty event handling process is as follows:
Private Sub Command1_Click ()
End Sub
As with this process, vb.net can also create an event-handling process by double-clicking a button control in the design environment, which is automatically generated by the system after you double-click a button control:

Private WithEvents Button1 as System.WinForms.Button
proteced Sub button1_click (Byval sender as System.object,_
                               Byval e as System.EventArgs)
End Sub

Although there are many differences from the code itself, event handling is the same. The new parameters are required for the control's event handling and, in most cases, users do not need to use them.
7. Give a class plus event
A user can declare an event of a class by using the events statement, declaring the name and parameters that contain the event.
First, you declare an event with a parameter by using the event statement in the declaration bar of the class module that defines the class, for example:
Public Event PercentDone (Byval Percent as Single,_
Byrefcancel as Boolean)
Events are always declared as public, and events do not return values, optional parameters, or array parameters.
Adding an event to a class illustrates that as long as an object of this class can trigger this particular event, the user needs to use the RaiseEvent expression to let an event actually run, and once the event has been run, Users can use WithEvents or AddHandler to link event and event handling processes.
8. Event Handling Process
Users can use the WithEvents and AddHandler keywords to write event-handling procedures.
Handling Events with WithEvents:
(1) In the Declaration column of the module that can handle the event, add the class variable with the keyword WithEvents, this variable must be a module-level variable.
(2) In the box to the left of the Code editing environment, select the variable declared with WithEvents.
(3) In the Drop-down box to the right of the Code editing environment, select the event that you want to handle (you can handle multiple events on an object if possible).
(4) Add the code to the event-handling process and use the relevant parameters.
To handle events using AddHandler:
AddHandler and RemoveHandler can be used together to achieve the function of WithEvents expression. But allows users to dynamically add, remove, and change error handling related to events. AddHandler has two parameters, the name of an event dispatcher (such as a control), and an expression that indicates a delegate relationship. When a user uses AddHandler, there is no need to explicitly declare a delegate class, and a reference to a delegate relationship can be returned by AddressOf. The following example links an event-handling process with a button's click event, which is derived from the creation of a new System.EventHandler instance.
AddHandlerButton1.Click.NewSystem.EventHandler (AddressOf
Me. Button1clickeventhandler
(1) A variable of a class is added to the declaration bar of the module to handle the event.
(2) Expressing and indicating the name of the event dispatcher with AddHandler, and the AddressOf Presentation code containing the event handling process. For example:
Dim h as New HH ()
AddHandler h.oevent,addressof me.handletheevent

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.