يطلق زر سياق Outlook الخاص بي (الوظيفة الإضافية) عدة مرات لكل نقطة

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

سؤال

لذا فقد حصلت على قائمة السياق هذه باستثناء أن الحدث وراء التحديد الفعلي لعنصر القائمة يبدو أنه يطلق عدة مرات. لأول مرة انقر فوقه ، يطلق النار مرة واحدة ، ثم مرتين ، ثم 3 مرات. لذلك ، في المثال الذي أعطيته للتو ، لمدة 3 نقرات كان من شأنه أن يطلق ما مجموعه 6 مرات (1 + 2 + 3). لماذا هذا؟

فيما يلي الكود الخاص بي حول كيفية إنشاء عناصر القائمة. لقد جردتها إلى القطع ذات الصلة ؛ لقد حذفت أشياء مثل .tag ،. أنا أقوم ببناء هذا باستخدام .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 ، استخدمت تعليقك الأخير كوسيلة للوصول إلى هذا الاستنتاج ، خاصةً حيث قلت:

قد يكون ذلك أن إنشاء عمليات اتصال إضافية (موقعة متطابقة)

الرمز الذي أستخدمه لإضافة المعالج هو:

AddHandler thisButton.Click, AddressOf OnContextClick

كيف يمكن أن يكون هذا مُعلَّم متشابهًا؟ حسنًا ، هناك واحد فقط OnContextClick الفرعي ... وماذا عن هذا ولكن هذا?

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 ، أضيف معالجًا إضافيًا لنفس الزر بالضبط ، ولا أتعتبر أن الزر يتم إنشاؤه بشكل أساسي في كل مرة يحدث فيها في كل مرة وأن معالجه يتم تخزينه في الذاكرة.

لذلك ، كنت بحاجة لجعل التحكم في الزر فريدًا:

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

لقد أضفت للتو الآن. الآن الأحداث فريدة من نوعها وإطلاقها الوحيد مرة واحدة لكل نقرة.

Tobrien ، أنا ذبابة لك! على الرغم من أنني أجبت في النهاية على سؤالي ، إلا أنه لم يخلو من إرشادك. شكرًا!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top