私のOutlookのコンテキストメニュー(アドイン)ボタン火災ワンクリックごとに複数回

StackOverflow https://stackoverflow.com/questions/2386964

質問

だから私は、メニュー項目の実際の選択の背後にあるイベントが複数回発射すると思われることを除いて、ワークアウト、このコンテキストメニューを持っています。私はそれをクリックして初めて、それは、かつて2回、その後、3回を発射します。だから、私はちょうど与えた例では、3回のクリックのためには、6回(1 + 2 + 3)の合計を解雇しているだろう。なぜそれがありますか?

以下は、私は、メニュー項目を作成していますどのように私のコードです。私は、関連する作品にそれを剥ぎ取ら。私は.TAG、.Visible、および.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安打で、私はボタンが基本的に新規にonUpdateが発生するたびに作成し、そのハンドラがメモリに格納されていることをされていることを考えていない、EXACT同じボタンのための追加のハンドラを追加している。

だから、私はボタンコントロールを一意にするために必要なます:

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

私はボタンがユーザに作成されるたびに、それが固有のタグを持っていることを確実にするために.TAGプロパティの末尾Now.ToBinary()。toString()を加えました。今イベントは一意であり、そのだけで、クリック一回発射ます。

tobrien、私はあなたを溶質!私は最終的には自分の質問に答えますが、それはあなたの指導がなかったわけではありません。ありがとう!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top