.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Articles >> Windows Application >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Handling Events By Attaching a Delegate

Posted By:Prasanti Prusty       Posted Date: February 27, 2010    Points: 25    Category: Windows Application    URL: http://www.dotnetspark.com  

Handling Events By Attaching a Delegate

Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object.
The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.

In Other Word :

A Delegate  is a function whose job is to Respond to an function.
A Delegate Has three Parts :
    1. AddHandler :
                    It is a Visual basic .Net Key word.
    2. Me.MouseDown :
                    It is the name of the event
    3. frmDelegates_MouseDown :
                    It Is the name of a delegate to handel the event

'Handling Events By Attaching a Delegate

'Code for Delegate
Public Class frmDelegates

    'Delegate to handel the MouseDown event
    Sub frmDelegates_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
        MessageBox.Show(String.Format("X = {0} , Y = {1}", e.X, e.Y), String.Format("the {0} Mouse Button hit me at : ", e.Button.ToString()))
    End Sub

    'Attach a Delegate for the event
    Private Sub frmDelegates_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler Me.MouseDown, AddressOf frmDelegates_MouseDown
    End Sub

End Class

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend