Archive for January, 2009

Handling Multiple Events With A Delegate

In a recent multi-tab VB.NET Web application, the values from a 3 controls on a tab (tab A) were used as filters for a dropdownlist on a subsequent tab (tab B).  If the controls on tab A were changed, the dropdownlist on tab B would be reloaded based on the changed value(s). 

In the event that all 3 controls changed, I didn’t want the dropdownlist reloaded 3 times, so I decide to use delegates as one solution.

' Fields 
Private Delegate Sub DentalBlueDelegate()
Private _dbd As DentalBlueDelegate
Private _mcd As [Delegate]

' Methods 
Private Sub ReloadDentalBlue()
   ' If the delegate does not have anything assigned the dental 
   ' blue delegate is added.  A multicast delegate is used to 
   ' prevent multiple calls to the same method from the different 
   ' TextChanged event handlers that need to trigger a reload of 
   ' the DentalBlue dropdownlist. 
   If _mcd Is Nothing Then
       _mcd = MulticastDelegate.Combine(_mcd, _dbd)
       _mcd.DynamicInvoke(Nothing)
   End If
End Sub

Private Sub txtParticipating_TextChanged(ByVal sender As Object, _
   ByVal e As System.EventArgs) Handles txtParticipating.TextChanged

   ReloadDentalBlue()
End Sub