views:

567

answers:

1

So I've got this context menu working out except that the event behind the actual selection of the menu item seems to fire multiple times. First time I click it, it fires once, then twice, then 3 times. So, in the example I just gave, for 3 clicks it would have fired a total of 6 times (1 + 2 + 3). Why is that?

Below is my code on how I'm creating the menu items. I stripped it down to the relevant pieces; I omitted things like .Tag, .Visible, and .Caption properties. I'm building this using .NET 3.5 and VS 2008.

Thanks in advance!!

Private WithEvents ActiveExplorerCBars As Office.CommandBars
Private app As New Outlook.Application

Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
     ActiveExplorerCBars = app.ActiveExplorer.CommandBars
     AddHandler ActiveExplorerCBars.OnUpdate, AddressOf ActiveExplorerCBars_OnUpdate
End Sub

//This seems to get hit A LOT    
Private Sub ActiveExplorerCBars_OnUpdate()
    Dim bar As Office.CommandBar

    If IgnoreCommandbarsChanges Then Exit Sub

    bar = ActiveExplorerCBars.Item("Context Menu")

    If Not bar Is Nothing Then
        Dim addMenu As Boolean = False
        //this For loop just makes sure the context is only available when the user right-clicks over a mail item
        For Each mail As Outlook.MailItem In Application.ActiveExplorer().Selection
            addMenu = True
            Exit For
        Next
        If addMenu Then
            AddContextDropdown(bar)
        End If
    End If
End Sub

Private Sub AddContextDropdown(ByVal ContextMenu As Office.CommandBar)
    Dim RootPopup As Office.CommandBarPopup
    Dim popupTaskItem As Office.CommandBarPopup
    RootPopup = ContextMenu.FindControl(Type:=Office.MsoControlType.msoControlPopup, Tag:="Update task")

    If RootPopup Is Nothing Then
        ChangingBar(ContextMenu, Restore:=False)
        RootPopup = ContextMenu.Controls.Add(Type:=Office.MsoControlType.msoControlPopup)

        Dim thisTaskPopup As Office.CommandBarPopup
        popupTaskItem = ContextMenu.FindControl(Type:=Office.MsoControlType.msoControlPopup, Tag:=task.EntryID)
        If popupTaskItem Is Nothing Then
              popupTaskItem = RootPopup.Controls.Add(Type:=Office.MsoControlType.msoControlPopup)
              thisTaskPopup = popupTaskItem
              AddActionButtons(thisTaskPopup)
        End If
    End If
End Sub

Private Sub AddActionButtons(ByVal puItem As Office.CommandBarPopup)

    Dim puDeploy As Office.CommandBarPopup = puItem.Controls.Add(Type:=Office.MsoControlType.msoControlPopup)
    Dim btnActionItem As Office.CommandBarControl = puDeploy.Controls.Add(Type:=Office.MsoControlType.msoControlButton)
    Dim thisButton As Office.CommandBarButton = btnActionItem
    AddHandler thisButton.Click, AddressOf OnContextClick
End Sub

//Click event
Public Sub OnContextClick(ByVal ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean)
   //This messagebox shows once the first time, twice the second, 3 times, etc
    MessageBox.Show("Clicked: " & ctrl.Caption)
End Sub
A: 

I figured it out. tobrien, I used your last comment as a vehicle to come to this conclusion, particularly where you said:

it could be that your actually creating ADDITIONAL (identically signitured) callbacks

The code I am using to add the handler is:

AddHandler thisButton.Click, AddressOf OnContextClick

How could this be identically signatured? Well, there is only one OnContextClick sub... so what about thisButton?

For Each value As ActivityType.Request In [Enum].GetValues(GetType(ActivityType.Request))
        Dim btnActionItem As Office.CommandBarControl = puRequest.Controls.Add(Type:=Office.MsoControlType.msoControlButton)
        With btnActionItem
            .Tag = puRequest.Tag & "|" & value
            .Caption = [Enum].GetName(GetType(ActivityType.Request), value)
            .Visible = True
        End With
        Dim thisButton As Office.CommandBarButton = btnActionItem
        AddHandler thisButton.Click, AddressOf OnContextClick
    Next

This code runs when OnUpdate occurs, which, as you know, happens ALL the time. So, in essence, each time OnUpdate hits, I'm adding an additional handler for the EXACT SAME BUTTON, not considering that the button is basically created newly each time OnUpdate occurs and that its handler is stored in memory.

So, I needed to make the button control unique:

.Tag = puRequest.Tag & "|" & value & "|" & Now.ToBinary().ToString()

I just added a Now.ToBinary().ToString() at the end of the .Tag property to ensure that each time the button is created to the user, it has a unique tag. Now the events are unique and its only firing once per click.

tobrien, I solute you! Although I ultimately answered my own question, it was not without your guidance. Thanks!

Honus Wagner