Understanding of events and commissioning (1) Use events

xiaoxiao2021-03-06  107

The event is the message sent by the object, and the occurrence of the signal notification operation. The object that causes (trigger) event is called an event sender. Capture the event and respond to the object of the event recipient.

In an event communication, the event sending party class does not know which object or method will receive (handle) the event it ranked. What is needed is that there is a medium (or the mechanism of the similar pointer) between the source and the reception. .NET Framework defines a special type (DELEGATE), which provides function pointer function.

The delegate is a class that can be referenced by methods. Unlike other classes, the delegate class has a signature, and it can only be referenced to the method that matches its signature. In this way, the commission is equivalent to a type of security function pointer or a callback. Although there are many other purposes, only the entrusted event processing functions are discussed here. The following example shows the event delegation declaration.

Event connection: To use events in the application, you must provide an event handler (Event Processing Method) that executes program logic to respond to events and register event handler to event source. We call this process to link. The application rapid development (RAD) tool provided by the Windows Forms and Web Forms Simplified (or hidden) The details of the event link. Click this process is the process of generating the following code Private Sub txtClient_SelectedIndexChanged (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtClient.SelectedIndexChanged ...... end subSystem.Web.UI.WebControls.Button control The event declaration is as follows. [C #] Public EventHandLer Click; [Visual Basic] Public Event Click As EventHandler'EventHandler is a delegate type To process the Click event, you must provide an event handler (event processing method) that signing EventHandler (Event Processing Method). The following code shows the EventHandler signature. [C #] Public Delegate Void EventHandler (Object Sender, Eventargs E); [Visual Basic] PUBLIC DELEGATE SUB EVENTHANDLER (Byval E AS Object, Byval E as Eventargs) 'This is what we usually say to define the method into a type according to the agreement. Events in .NET Framework have two parameters: the source of the event and the data of the event. The event data class is exported from System.EventArgs. If the event does not generate data, it uses Eventargs as an event data type.

Event mode Although the specific situation of the event link in the Windows form and web form is different (the reason is different from the support level provided by the different RAD tools), both cases follow the same event mode, which has the following characteristics . The class that causes the EventName event has the following members. [C #] Public eventnameEventhandler eventname; [Visual Basic] Public EventName As EventNameEventHandleReventName event's event delegate is EventNameEventHandler, with the following signatures. [C #] public delegate void EventNameEventHandler (object sender, EventNameEventArgs e); [Visual Basic] Public Delegate Sub EventNameEventHandler (sender As Object, e As EventNameEventArgs) To use EventName event, your event handler must have the same signature as the event delegate . [C #] void EventHandler (Object Sender, EventNameEventAndargs E) {} [Visual Basic] Sub EventHandler (Sender AS Object, EventNameEventAndler) The Click event in the example is not associated. It uses Eventargs classes to event data and uses EventHandler as a delegate. Events with associated data use the class exported from Eventargs in the part data type and the corresponding event commission type. For example, if you want to handle the MouseUp event in the Windows Form Application, the event data classes are MouseEventArgs, and the event delegate is MouseEventHandler. Note that some mouse events use the public class and public events of event data, so the naming scheme is not fully matched with the agreement described above. Your event handler must have the following signatures. [C #]

Void Mouse_Moved (Object Sender, MouseEventArgs E) {}

[Visual Basic]

SUB Mouse_Moved (Sender As Object, e as mouseeventargs)

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

New Post(0)