Visual Studio Developer Center > Visual Studio Forums > Visual Studio Tools for Office > Why does my command bar button’s click event only fires for the first time?
Ask a questionAsk a question
 

LockedWhy does my command bar button’s click event only fires for the first time?

Locked

  • Sunday, February 08, 2009 12:24 PMJi.ZhouMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Why does my command bar button’s click event only fires for the first time?

     
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Answers

  • Sunday, February 08, 2009 12:24 PMJi.ZhouMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
     

    This scenario usually happens when we declare the command bar button as a temporary variable in the function scope. After the function finishes, the command bar button is no longer in scope and which causes it to be released by GC. As a result, the click event will not fire for a collected object.

    The solution for this issue is declaring the command bar button in a class level scope, so that it will not be collected by the GC thread. Note the Tag property is also important and must be set in this case because it is used by Office application to trace the button at runtime. Code will look like this:

        public partial class ThisAddIn

        {

            Office.CommandBar cb;

            Office.CommandBarButton btn;   //Declare the button in class level scope.

     

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

            {

                cb = this.Application.ActiveExplorer().CommandBars.Add("test", Office.MsoBarPosition.msoBarTop,

                    false, true) as Office.CommandBar;

                cb.Visible = true;

     

                btn = cb.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true) as Office.CommandBarButton;

                btn.Caption = "My Button";

                btn.Tag = "My Button";   //Do not forget to set the Tag property

                btn.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(btn_Click);

            }

            …

    }

    (Related forum thread, http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/dda9c1c7-d5d4-4f94-b60a-070f99becb6f/ )



    For more FAQ about Visual Studio Tools for Office, please see Visual Studio Tools for Office FAQ


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

All Replies

  • Sunday, February 08, 2009 12:24 PMJi.ZhouMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
     

    This scenario usually happens when we declare the command bar button as a temporary variable in the function scope. After the function finishes, the command bar button is no longer in scope and which causes it to be released by GC. As a result, the click event will not fire for a collected object.

    The solution for this issue is declaring the command bar button in a class level scope, so that it will not be collected by the GC thread. Note the Tag property is also important and must be set in this case because it is used by Office application to trace the button at runtime. Code will look like this:

        public partial class ThisAddIn

        {

            Office.CommandBar cb;

            Office.CommandBarButton btn;   //Declare the button in class level scope.

     

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

            {

                cb = this.Application.ActiveExplorer().CommandBars.Add("test", Office.MsoBarPosition.msoBarTop,

                    false, true) as Office.CommandBar;

                cb.Visible = true;

     

                btn = cb.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true) as Office.CommandBarButton;

                btn.Caption = "My Button";

                btn.Tag = "My Button";   //Do not forget to set the Tag property

                btn.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(btn_Click);

            }

            …

    }

    (Related forum thread, http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/dda9c1c7-d5d4-4f94-b60a-070f99becb6f/ )



    For more FAQ about Visual Studio Tools for Office, please see Visual Studio Tools for Office FAQ


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • Wednesday, April 01, 2009 11:50 AMJi.ZhouMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Add VB version codes,

    Public Class ThisAddIn
        Dim cb As Office.CommandBar
        Dim btn As Office.CommandBarButton  'Declare the button in class level scope

        Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
            cb = CType(Me.Application.ActiveExplorer().CommandBars.Add("test", Office.MsoBarPosition.msoBarTop, False, True), Office.CommandBar)
            cb = Me.Application.ActiveExplorer().CommandBars.Add("test", Office.MsoBarPosition.msoBarTop, False, True)
            cb.Visible = True

            btn = CType(cb.Controls.Add(Office.MsoControlType.msoControlButton, , , , True), Office.CommandBarButton)
            btn.Caption = "My Button"
            btn.Tag = "My Button"   'Do not forget to set the Tag property
            AddHandler btn.Click, AddressOf btn_Click  
    End Sub
    End Class


    We have published a VSTO FAQ recently, you can view them from the entry thread http://social.msdn.microsoft.com/Forums/en/vsto/thread/31b1ffbf-117b-4e8f-ad38-71614437df59. If you have any feedbacks or suggestions on this FAQ, please feel free to write us emails to colbertz@microsoft.com.