Visio Guy

Visio Discussions => Programming & Code => Topic started by: perry59 on October 26, 2016, 10:56:20 PM

Title: which way to the event?
Post by: perry59 on October 26, 2016, 10:56:20 PM
I have seen two methods (in vb.net) to implement event handling.
one is using the event sink methodology which seems convoluted and more difficult. The other method is using "AddHandler" which seems pretty straight forward. (example below)
is one method better than the other? why would I use one over the other?

here is some trimmed code from "treeview" in the SDK


Private documentClosedEvent As [Event]

        Private Sub addAdviseEvents()
            Dim applicationEventList As EventList
            applicationEventList = visioApplication.EventList
             ' Listen to DocumentClosed event.
            If (documentClosedEvent Is Nothing) Then
                documentClosedEvent = applicationEventList.AddAdvise( _
                    VisEventCodes.visEvtDel + VisEventCodes.visEvtDoc, _
                    Me, "", "")
            End If
        End Sub

        Private Sub handleDocumentClosed(ByVal closedDocument As Document)
            Dim taskDrawing As TaskDrawing
            taskDrawing = Me.getTaskDrawing(closedDocument)
            If (Not taskDrawing Is Nothing) Then
                taskDrawing.Dispose()
                documentDrawingHash.Remove(closedDocument)
            End If
            If (documentDrawingHash.Count = 0) Then
                unAdviseEvents()
                deleteCustomMenu()
            End If
        End Sub

        Protected Function ProcessVisioEvent(ByVal eventCode As Short, _
            Try
                If (visioApplication.IsUndoingOrRedoing = True) Then
                    Return False
                End If
                Select Case localEvent
                    Case CShort(VisEventCodes.visEvtDel _
                        + VisEventCodes.visEvtDoc)
                        ' A document has been closed.
                        documentSubject = CType(subject, Document)
                        handleDocumentClosed(documentSubject)
                End Select
            Catch err As ApplicationException
                LogException(err)
                DisplayException(visioApplication, err)
            Catch err As System.Runtime.InteropServices.COMException
                LogException(err)
                DisplayException(visioApplication, err)
            End Try
            Return False
        End Function

        Private Sub unAdviseEvents()
            If Not (documentClosedEvent Is Nothing) Then
                documentClosedEvent.Enabled = 0
                documentClosedEvent.Delete()
                Marshal.ReleaseComObject(documentClosedEvent)
                documentClosedEvent = Nothing
            End If
        End Sub


and here is some from my addin using Nikolay's template. This is a LOT easier to understand and seems to work just fine!


Public Sub New(addIn As ThisAddIn)
        ThisAddIn = addIn
        AddHandler ThisAddIn.Application.DocumentOpened, AddressOf OnDocumentOpened
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
        RemoveHandler ThisAddIn.Application.DocumentOpened, AddressOf OnDocumentOpened
    End Sub

Private Sub OnDocumentOpened(doc As Microsoft.Office.Interop.Visio.Document)
'do your stuff here
end sub
Title: Re: which way to the event?
Post by: perry59 on October 28, 2016, 02:58:29 PM
anyone? Bueller?
Title: Re: which way to the event?
Post by: wapperdude on October 28, 2016, 03:49:55 PM
Sorry Perry59, I have no experience with this. 

Wapperdude
Title: Re: which way to the event?
Post by: perry59 on October 28, 2016, 06:22:29 PM
Quote from: wapperdude on October 28, 2016, 03:49:55 PM
Sorry Perry59, I have no experience with this. 

Wapperdude

Thanks!
there must be someone out there smarter than us :o
Title: Re: which way to the event?
Post by: wapperdude on October 28, 2016, 07:06:39 PM
Smarter???   :o  Nah!   ::)   More experienced... ;)

;D

Wapperdude
Title: Re: which way to the event?
Post by: Visisthebest on February 05, 2023, 09:40:07 PM
Still a great question hope someone will answer it!
Title: Re: which way to the event?
Post by: Visisthebest on February 06, 2023, 09:09:45 AM
Perry59 you could also ask this question in the Stackoverflow forum.