none
CommandBarButton Click Event calls multiple time RRS feed

  • Question

  • More info here
    http://www.add-in-express.com/forum/read.php?FID=5&TID=2922

    I try to follow the information in that page, but for C# that never worked. I get the following error while removing the event.

    Description:
    System.NullReferenceException: Object reference not set to an instance of an object.
       at Microsoft.Office.Core._CommandBarButtonEvents_EventProvider.remove_Click(_CommandBarButtonEvents_ClickEventHandler )
       at Microsoft.Office.Core.CommandBarButtonClass.remove_Click(_CommandBarButtonEvents_ClickEventHandler )
       at ThisAddIn.SendToPublicFolder_Click(CommandBarButton Ctrl, Boolean& CancelDefault) in ....\ThisAddIn.cs:line 197

    For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.


    I also see that there is SinkHelper for Office.Core related to CommandBarButton, so I'd like to know if this is a bug or just by designed.

    My current fix for this is to have counter to validate if the method will be call more than once, and if so, to exit the event. This is a very ugly fix, so let me know if you have a fix for that.

    I'm using VS.NET 2005 SP1, Office 2007, and this is an Outlook Add-In project.

    Thanks,
    Rogelio Morrell C.
    Microsoft MVP
    Admios
    Panama
    Wednesday, April 9, 2008 9:00 PM

Answers

  • Hi,

     

    This issue happens because every time ItemContextMenuDisplay is called, we registered the button’s Click event for one time. And the last event is not collected by GC.

    Try the following codes which work fine on my pc.

    Code Snippet

            Office.CommandBarButton button = null;

     

            private void ThisAddIn_Startup(object sender, System.EventArgs e)

            {

                this.Application.ItemContextMenuDisplay += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemContextMenuDisplayEventHandler(Application_ItemContextMenuDisplay);

            }

     

            void Application_ItemContextMenuDisplay(Microsoft.Office.Core.CommandBar CommandBar, Microsoft.Office.Interop.Outlook.Selection Selection)

            {

                button = null;

                GC.Collect();

     

                button = CommandBar.Controls.Add(Office.MsoControlType.msoControlButton,

                missing, missing, missing, missing) as Office.CommandBarButton;

                button.Caption = "My Test Button";

                button.Tag = "My Test Button";

                button.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption;

                button.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(button_Click);

            }

     

            void button_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)

            {

                MessageBox.Show("test");

            }

     

     

     

    Thanks,

    Ji

     

    Tuesday, April 15, 2008 6:24 AM
    Moderator