Wednesday, May 21, 2008

how to raise and handle events/callbacks in .net?

How to implement and respond to Events in .net ?

In the following example, you'll create a class library containing a class that raises a pair of events. Then you'll create a user interface application that can respond to those events. You'll use both compile time event association with Handles and run-time event association with AddHandler.

Create the Class Library
Follow these steps to create the class library that will raise the events:

Open Microsoft® Visual Studio® .Net, and on the Start Page, click New Project.
On the tree view on the left-hand side of the screen, click Visual Basic Projects.
Click Class Library to select it as the project template.
Set the name of the application to Water and click OK to create the project.
Click the class called Class1.vb in the Solution Explorer window and rename it to Bucket.vb.
Select the code for Class1 in Bucket.vb (this will be an empty class definition) and replace it with the following code:

Class Bucket

Private ContentsValue As Integer
Private CapacityValue As Integer
Private NameValue As String

Public Event Full()
Public Event Overflowing(_
ByVal sender As System.Object)

Public Sub New(ByVal Capacity As Integer)
mintCapacity = Capacity
mintContents = 0
End Sub

Public Sub Empty()
mintContents = 0
End Sub

Public ReadOnly Property Contents()
Get
Contents = mintContents
End Get
End Property

Public Property Name() As String
Get
Name = mstrName
End Get
Set(ByVal Value As String)
mstrName = Value
End Set
End Property

Public Sub Add(ByVal Amount As Integer)
mintContents = mintContents + Amount
If mintContents > mintCapacity Then
RaiseEvent Overflowing(Me)
mintContents = mintCapacity
ElseIf mintContents = mintCapacity Then
RaiseEvent Full()
End If
End Sub

End Class

This code creates a class named Bucket to represent a bucket that can be filled with water. The New method initializes the bucket with a specified capacity. The Name property assigns a name to the bucket. The Add method adds water to the bucket, and the Empty method empties the bucket.

The Bucket class includes two event declarations:

Public Event Full()
Public Event Overflowing(_
ByVal sender As System.Object)

These two events are both raised within the Add method. If the amount of water in the bucket exceeds the capacity of the bucket, the code raises the Overflowing event. Otherwise, if the amount of water in the bucket exactly equals the capacity of the bucket, the code raises the Full event.

Note that the argument lists of the two events are different. You have complete flexibility to pass whatever information you need in any particular event.

On the Build menu, click Build or press Ctrl+Shift+B to build the class library.
Create the User Interface Project
Follow these steps to create a Windows Application to test the events of the Bucket class:

Open Visual Studio .Net and on the Start Page, click New Project.
On the tree view on the left-hand side of the screen, click Visual Basic Projects.
Click Windows Application to set it as the project template.
Set the name of the application to BucketTest and click OK to create the project.
Select the form called Form1.vb in the Solution Explorer window and rename it to frmBuckets.vb.
Create the form by adding the appropriate controls and setting the properties of those controls.

Add Code to Handle Events
Now you're ready to write code to handle the events from the Bucket class. This code will create three objects of the Bucket class. The Full events will be individually connected to event handlers at compile time using Handles. The Overflow events will all be handled by a single procedure, dynamically associated with the events at run time by using AddHandler.

On the View menu, click Code, and enter this code before the Windows Form Designer generated code:

Dim WithEvents Bucket1 As New Water.Bucket(10)
Dim WithEvents Bucket2 As New Water.Bucket(10)
Dim WithEvents Bucket3 As New Water.Bucket(10)

Now enter this code after the Windows Form Designer generated code:

Private Sub Form1_Load(_
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Bucket1.Name = "Bucket 1"
Bucket2.Name = "Bucket 2"
Bucket3.Name = "Bucket 3"
AddHandler Bucket1.Overflowing, _
AddressOf HandleOverflow
AddHandler Bucket2.Overflowing, _
AddressOf HandleOverflow
AddHandler Bucket3.Overflowing, _
AddressOf HandleOverflow
End Sub

Private Sub btnAdd1_Click(_
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnAdd1.Click
Bucket1.Add(1)
pb1.Value = Bucket1.Contents
End Sub

Private Sub btnAdd2_Click(_
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnAdd2.Click
Bucket2.Add(1)
pb2.Value = Bucket2.Contents
End Sub

Private Sub btnAdd3_Click(_
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnAdd3.Click
Bucket3.Add(1)
pb3.Value = Bucket3.Contents
End Sub

Private Sub Bucket1_Full() Handles Bucket1.Full
lblFull1.Visible = True
End Sub

Private Sub Bucket2_Full() Handles Bucket2.Full
lblFull2.Visible = True
End Sub

Private Sub Bucket3_Full() Handles Bucket3.Full
lblFull3.Visible = True
End Sub

Private Sub HandleOverflow(_
ByVal sender As System.Object)
lboOverflow.Items.Add(sender.Name & " Overflow!")
End Sub

Read more on this msdn site where you will find this example and details - :
http://msdn.microsoft.com/en-us/library/ms973905.aspx

No comments: