Frage

Also habe ich dieses Kontextmenü bekam außer der Arbeit aus, dass das Ereignis hinter der tatsächlichen Auswahl des Menüpunktes erscheint mehrmals zu schießen. Erstes Mal, dass ich es klicken, es feuert einmal, dann zweimal, dann 3 mal. So wird in dem Beispiel gab ich gerade, für 3 Klicks würde es insgesamt 6 mal gefeuert hat (1 + 2 + 3). Warum das?

Unten ist mein Code auf, wie ich die Menüpunkte bin zu schaffen. Ich zog es nach unten auf die entsprechenden Stücke; Ich weggelassen Dinge wie .Tag, .Visible und .Caption Eigenschaften. Ich baue die .NET 3.5 und VS 2008 verwenden.

Vielen Dank im Voraus !!

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
War es hilfreich?

Lösung

dachte ich es aus. tobrien, habe ich Ihre letzte Bemerkung als ein Fahrzeug zu diesem Schluss zu kommen, vor allem, wenn Sie sagte:

  

könnte es sein, daß Ihr ZUSÄTZLICHE tatsächlich zu schaffen (identisch signitured) Rückrufe

Der Code, den ich die Handler hinzufügen bin mit ist:

AddHandler thisButton.Click, AddressOf OnContextClick

Wie diese identisch signatured werden könnte? Nun, es ist nur ein OnContextClick Unter ... so etwas ist 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

Dieser Code wird ausgeführt, wenn OnUpdate auftritt, was, wie Sie wissen, die ganze Zeit passiert. Also, im Grunde jedes Mal OnUpdate Hits, ich bin das Hinzufügen einer zusätzlichen Prozedur für die exakt gleiche BUTTON, nicht bedenkt, dass die Schaltfläche im Grunde jedes Mal neu erstellt wird OnUpdate auftritt und dass seine Handler im Speicher gespeichert wird.

Also, ich brauchte die Tastensteuerung einzigartig zu machen:

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

Habe ich nur noch eine Now.ToBinary (). ToString () am Ende der .Tag Eigenschaft, um sicherzustellen, dass jedes Mal, wenn die Taste für den Benutzer erstellt wird, ist es einen eindeutigen Tag hat. Jetzt sind die Ereignisse einzigartig und es ist nur einmal pro Klick zu schießen.

tobrien, ich solute Sie! Obwohl ich schließlich meine eigene Frage beantworten, war es nicht ohne Ihre Führung. Dank!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top