How to: Identify Characteristics of Events in VB.NET

No comments - This post in romanian

The event is a message sent by an object and announces the application of production a fact.
Events are implemented using delegates [...].
For example, clicking a command button in a graphical interface causes the appearance of the event Click. The event is treated by a procedure.

Events are declared in the classes, structures, modules and interfaces using the key word Event.

Event Eveniment(ByVal IDArg As Integer)

Transmission the message associated to an event is called raising, and it is realized in the class, module or structure. A derived class objects cannot cause transmission events inherited from base class. Specification used for transmitting the message is RaiseEvent.

RaiseEvent Eveniment(IDArg)

An object capable of causing transmission of a message event is called the source of the event or event emitter. Examples of event source objects are objects defined by the user, controls and forms.
An event occurrence is associated with a procedure called an event handler. Before an event handler can be used, must be made an association between event and event handler. This is achieved using specifications Handles or AddHandler.
Event handlers are declared with WithEvents and Handles specifications. Events caused by an object through WithEvents can be manipulated by any subroutine with Handles specification that specifies the event. Pair event-handler with Handles specification is limited by achieve the association at compile-time.
A variable defined with WithEvents specification can be used as an Object variable, specifying the class name at defining-time.

Dim WithEvents ObEveniment As New ClsEveniment

ObEveniment is a variable declared wit WithEvents specification, and class ClsEveniment contains statements of events and procedures for message transmission associated to the events.
Declare an event handler using the Handles specification by associating themselves with events declared in ClsEveniment.

Sub ObEveniment_HandlerEveniment() Handles ObEveniment.UnEveniment

UnEveniment event is declared in class ClsEveniment.
AddHandler and RemoveHandler specifications are more flexible than Handles clause. It is dynamically allowed to connect and disconnect the events with one or more handlers and the running-time of the application. Object variables do not need to be declared with WithEvents specification.
AddHandler takes two arguments: the name of an event from an event source and an expression evaluated to delegate.

AddHandler ObEveniment.EvenimentDoi, AddressOf Me.HandlerEveniment

RemoveHandler disconnects an event from an event handler and uses the same syntax as AddHandler.

RemoveHandler ObEveniment.EvenimentDoi, AddressOf Me.HandlerEveniment

AddressOf specification returns the address of the delegate.

, , , ,


  1. No comments yet.
(will not be published)

  1. No trackbacks yet.