1

したがって、メニュー項目の実際の選択の背後にあるイベントが複数回発生するように見えることを除いて、このコンテキストメニューは機能しています。初めてクリックすると、1回、2回、3回の順に発火します。したがって、先ほど示した例では、3回のクリックで、合計6回(1 + 2 + 3)発火します。何故ですか?

以下は、メニュー項目の作成方法に関する私のコードです。私はそれを関連する部分に分解しました。.Tag、.Visible、.Captionプロパティなどを省略しました。これは、.NET3.5とVS2008を使用して構築しています。

前もって感謝します!!

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
4

1 に答える 1

1

私はそれを考え出した。tobrien、私はあなたの最後のコメントを、特にあなたが言ったところで、この結論に到達するための手段として使用しました:

実際に追加の(同じように署名された)コールバックを作成している可能性があります

ハンドラーを追加するために使用しているコードは次のとおりです。

AddHandler thisButton.Click, AddressOf OnContextClick

どうすればこれを同じように署名できますか?ええと、OnContextClickサブは1つだけです...では、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が発生するたびにボタンが基本的に新しく作成され、そのハンドラーがメモリに格納されることを考慮せずに、まったく同じボタンのハンドラーを追加します。

したがって、ボタンコントロールを一意にする必要がありました。

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

.Tagプロパティの最後にNow.ToBinary()。ToString()を追加して、ボタンがユーザーに作成されるたびに一意のタグが付けられるようにしました。現在、イベントは一意であり、クリックごとに1回だけ発生します。

トブリエン、私はあなたを溶かします!私は最終的に自分の質問に答えましたが、それはあなたの指導なしではありませんでした。ありがとう!

于 2010-03-09T15:04:52.453 に答える