Click Handler on CommandBarButton Not Working

Feb 15, 2013 at 7:53 PM

I've got a problem with Click handler on my CommandBarButton created in classic UI. Event is simply not raised. I tried every possible combination which came to my mind to make it work but I ran out of ideas without any success.

Button is successfully created (or found at CommandBar).

I use this method to create my button:
    Private WithEvents addTaskButton As CommandBarButton

    Public Sub AddTask_ClassicClicked(control As CommandBarButton, ByRef cancelDefault As Boolean) Handles addTaskButton.ClickEvent
        ' not called:
        control.Caption = "Clicked"
        System.Windows.Forms.MessageBox.Show("Classic clicked!")

        ' release COM object
    End Sub

    Private Sub CreateUserInterface()
        ' COM proxy object
        Using commandBars As CommandBars = Me.Application.ActiveExplorer.CommandBars

            ' Create a toolbar button on the standard toolbar
                ' See if it already exists
                Dim missing = System.Reflection.Missing.Value
                Me.addTaskButton = DirectCast(commandBars("Standard").FindControl(MsoControlType.msoControlButton, missing, ADD_TASK_BUTTON_TAG), CommandBarButton)

            Catch ex As System.Exception
            End Try

            If Me.addTaskButton Is Nothing Then
                ' Create it
                Dim missing = System.Reflection.Missing.Value
                Me.addTaskButton = DirectCast(commandBars("Standard").Controls.Add(MsoControlType.msoControlButton, missing, missing, missing, True), CommandBarButton)

                ' Configure it
                Me.addTaskButton.Caption = "Add Task"
                Me.addTaskButton.DescriptionText = "Coverts currently selected mail messages into tasks."
                Me.addTaskButton.TooltipText = "Coverts currently selected mail messages into tasks."
                Me.addTaskButton.Picture = PictureDispConverter.Convert(My.Resources.Tasks_16)
                Me.addTaskButton.Mask = PictureDispConverter.Convert(My.Resources.Tasks_16_mask)

                Me.addTaskButton.Style = MsoButtonStyle.msoButtonIconAndCaption
            End If

                ' add click handler
                Dim clickHandler As NetOffice.OfficeApi.CommandBarButton_ClickEventHandler = AddressOf Me.AddTask_ClassicClicked
                AddHandler Me.addTaskButton.ClickEvent, clickHandler
            Catch ex As System.Exception
            End Try

            Me.addTaskButton.Tag = ADD_TASK_BUTTON_TAG
            Me.addTaskButton.OnAction = "!<" & Constants.PROG_ID & ".Connect>"

            Me.addTaskButton.Enabled = True
            Me.addTaskButton.Visible = True
        End Using
    End Sub
Testing on Office 2007 Professional (Outlook), 32bit version; Windows XP 32bit.

Do you have any ideas to make it work?

Feb 16, 2013 at 4:46 PM
did you keep the Button alive? (use the OnDispose event for debugging)

Feb 18, 2013 at 2:25 PM
Thanks for suggestion!

Problem was caused by Using block after which button was disposed. After removal event is fired correctly!