How to use Delegates in Winforms

Delegates are a tool to raise an event from one class to another, from one form to another, or any combination.

Let's say that you have a form that shows customers, and a popup dialog that gives you a grid of customers to choose from. We'd like to raise an event in the frmChooseCustomer to let frmCustomerEdit know that a customer has been chosen. So, lets start in frmChooseCustomer:

There are four tasks:

Related Articles

... and you 'll find more on the NET Development Menu

Public Class SalespersonLookup
    'declare a delegate
    'this is a 'template'
    Public Delegate Sub gridRowDoubleClicked(ByVal strSalespersonID As String)
 
    'create a public instance of the delegate
    'this is 'live'
    Public NotifyRowDoubleClicked As gridRowDoubleClicked
 
    'In the grid doubleclicked event (or however the customer number is selected), do this:
    Private Sub RadGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles RadGridView1.CellDoubleClick
 
        Try
            'invoke the delegate
            NotifyRowDoubleClicked.Invoke(e.Row.Cells("slprsnid").Value)
            Me.Close()
 
        Catch ex As Exception
            FPCommon.ErrorHandler.globalErrorHandler(ex, True)
        End Try
 
    End Sub
 
    Private Sub SalespersonsLookup_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'FP_RM00301_SEL2
        Me.RadGridView1.AllowAddNewRow = False
        Me.RadGridView1.AllowEditRow = False
        Me.RadGridView1.EnableGrouping = False
        Me.RadGridView1.EnableFiltering = True
        Me.RadGridView1.ShowFilteringRow = True
 
        Dim oTelerikGrid As New TelerikGrid
        Me.RadGridView1.Columns.Add(oTelerikGrid.createGridViewTextBoxColumn("Salesperson", "slprsnid", 15, 150))
        Me.RadGridView1.DataSource = dynData.SPs.FP_RM00301_SEL2(appUser.Db).getTable
    End Sub
 
End Class

 

So, we're juggling three objects here - the delegate, the local instance of the delegate, and the sub that is attached to.

 In the calling form (frmCustomerEdit), we do this:

Private Sub btnSalespersonLookup_Click(sender As System.Object, e As System.EventArgs) Handles btnSalespersonLookup.Click
        'declare a lookup form
        Dim frm As New SalespersonLookup
 
        'register for the delegate on that form.
        'in other words, when 'NotifyRowDoubleClicked' is invoked, 'SalespersonChosen' will handle the event
        frm.NotifyRowDoubleClicked = AddressOf SalespersonChosen
 
        'show the form modally
        frm.ShowDialog()
    End Sub
 
 
    Sub SalespersonChosen(strSalespersonID As String)
        'this fires when the 'NotifyRowDoubleClicked' delegate on the other form is invoked
        Me.txtSalesperson.Text = strSalespersonID
    End Sub

In my case, the delegate has one string parameter. It can have zero or as many params as you want, of any type.

As always, comments are welcome

 

 

 

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences