Advancing event handling from VB6 to VB.NET


Summary
This topic shows ArcGIS developers how to take advantage of the .NET Framework best practices for event handling when advancing their custom applications from Visual Basic 6 (VB6) to Visual Basic .NET (VB .NET).

About advancing event handling

In VB6, event handling is performed using the WithEvents keyword and writing code for specific event handlers. VB .NET continues to honor this VB6 programming practice for legacy purposes but a better mechanism exists for event handling. The preferred VB .NET event handling mechanism is to specify a particular event interface, perform a Cast, then register the event handler using the AddHandler and AddressOf keywords. For a good starting point for learning how to perform these tasks in VB .NET, see Handling and Raising Events on the Microsoft Developer Network (MSDN) Web site.
The following is the basic premise of event handling in VB6:
  • Use the WithEvents keyword to declare a variable.
  • Hook up the sink and the event source.
VB6 only supports one outbound interface per coclass. Developers must use a dummy coclass to access other outbound interfaces.
The following is a simple code example of VB6 event handling:
Private WithEvents m_pActiveViewEvents As Map

Private Sub WireUpEvent(ByVal pMxDoc As IMxDocument)
 'Wire up the event handler.
  m_pActiveViewEvents = pMxDoc.FocusMap
End Sub

Private Sub m_pActiveViewEvents_SelectionChanged()
  'Fires when the SelectionChanged Event occurs.
End Sub
The following is the basic premise of event handling in VB .NET:
  • Use an ArcObjects xxxxxxxxxx_Event interface to declare a variable.
  • Cast the event interface.
  • Register the event handler method via the AddHandler and AddressOf keywords.
The following is a simple code example of VB .NET event handling:
[VB.NET]
Private m_map_Events As IActiveViewEvents_Event

Public Sub WireUpEvent (ByVal map As IMap)
    'Wire up the map's event handler.
    m_map_Events = CType(map.FocusMap, IActiveViewEvents_Event)
    
    'Create an instance of the delegate, then add it to SelectionChanged.
    AddHandler m_map_Events.SelectionChanged, AddressOf CustomSelectionChanged
End Sub

Private Sub CustomSelectionChanged()
    'Fires when the SelectionChanged Event occurs.
End Sub
The RemoveHandler statement can be used to unwire an event dynamically (this feature cannot be done in VB6). In VB .NET, a dummy coclass is not needed for outbound interfaces. Delegates are the mechanism used to communicate between events. ArcGIS developers need to manually add the _Event suffix to the name of the particular event interface. 
In prior VB6 to .NET migration topics, it was espoused to remove as many member (that is, global) variables as possible from your VB6 code to simplify the migration process when the auto-translation from VB6 to VB .NET occurred. When it comes to wiring events in VB .NET using the AddressOf, AddHandler, and RemoveHandler statements, developers almost always need to use a member variable because the events can fall out of scope without knowing it and can cause unexpected behavior.


See Also:

Migrating from VB6 to VB.NET
General steps for migrating from VB6 to VB.NET




Development licensing Deployment licensing
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced
Engine Developer Kit Engine