所以我有这个右键菜单,不同的是菜单项的实际选择背后的事件似乎触发多次。第一次我点击它,它触发一次,然后两次,然后3次。所以,我只是给,为3次点击的例子中它会一共有6次(1 + 2 + 3)解雇。为什么呢?

下面是我对我如何创建菜单项的代码。我剥离下来的相关件;我省略之类的东西.TAG,。可见,和.Caption性能。我建立这个使用.net 3.5和VS 2008。

在预先感谢!!

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
有帮助吗?

解决方案

我计算出来。 tobrien,我用了你最后的评论作为一种手段,得出这样的结论,特别是在对你说:

  

这可能是因为你的实际创建额外的(相同signitured)回调

我使用添加的处理程序中的代码是:

AddHandler thisButton.Click, AddressOf OnContextClick

这怎么可能进行相同signatured?那么,只有一个 OnContextClick 子......那么,关于 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

当的OnUpdate发生,其中,因为大家知道,发生的时间该代码运行。因此,在本质上,每个时间的OnUpdate命中,我添加一个额外的处理程序完全相同的BUTTON,不考虑该按钮被新每次基本上创建的OnUpdate发生和其处理程序被存储在存储器中。

所以,我需要使按钮控制独特:

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

我只是在.TAG属性的末尾添加一个Now.ToBinary()。的ToString(),以确保每个按钮被创建到用户时,它具有独特的标记。现在,事件是独一无二的,它唯一的每次点击射击一次。

tobrien,我溶质您!虽然我最终回答了我的问题,这也不是没有你的指导。谢谢!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top