Login    
 
 
 
 
Text/HTML
  
You are here :: Blogs Saturday, May 19, 2012

Search
Note: This uses the internal blog search engine. The Google search engine is also available at the top of the page.
  
Disclaimer

Please review the site disclaimer before downloading or using content found on this site

  
Categories
  
DEVSHED Blog
As always, I welcome your comments!
Jun 22

Written by: Steve Gray
6/22/2009 1:29 PM  RssIcon

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

Lets 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:

'declare a delegate
Public Delegate Sub gridRowDoubleClicked(ByVal strCustomerNumber As String) 
'create a local instance of the delegate
Private RowDoubleClicked As gridRowDoubleClicked
 
'create a sub that the calling form can attach to
Public Sub NotifyRowDoubleClicked(ByVal value As gridRowDoubleClicked)
    RowDoubleClicked = value
End Sub
 
'In the grid doubleclicked event (or however the customer number is selected), do this:
Private Sub GridEX1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridEX1.DoubleClick
 
    Dim strSopnumbe As String = ""
    Try
        Dim currentRow As Janus.Windows.GridEX.GridEXRow = Nothing
        currentRow = GridEX1.GetRow()
 
        RowDoubleClicked.Invoke(currentRow.Cells.Item("custnmbr").Text)
        Me.Close()
 
    Catch ex As Exception
        globalErrorHandler(ex)
    End Try
 
End Sub
 

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 btnOrderLookup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOrderLookup.Click
 
 
    'create an instance of the 'choose customer' form
    Dim frm As New SOPChooseCustomer 
    'attach to the notify sub
    frm.NotifyRowDoubleClicked(New SOPChooseCustomer.gridRowDoubleClicked(AddressOf customerChosen))
 
 
    'open the form modally
    frm.ShowDialog() 
End Sub
 
 
'notice that the signature is the same as the DELEGATE
Public Sub customerChosen(ByVal strCustomerNumber As String)
 
 
    'this fires when the 'choose customer' fires the 'doubleClicked' event
    Me.txtCustomerNumber.Text = strCustomerNumber 
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

Tags:
Categories:
As always, I welcome your comments!
  
 
 
Home | Products | Blogs | Contact Us | Links | God's Plan
Privacy Statement | Terms Of Use
 
Copyright 2011 by Devshed.us