Why does my command bar button’s click event only fires for the first time?
Locked
- 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.- Edited byJi.ZhouMSFT, ModeratorWednesday, February 11, 2009 12:42 PM
- Edited byJi.ZhouMSFT, ModeratorFriday, February 13, 2009 10:52 AM
Answers
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.- Marked As Answer byJi.ZhouMSFT, ModeratorWednesday, February 11, 2009 12:42 PM
All Replies
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.- Marked As Answer byJi.ZhouMSFT, ModeratorWednesday, February 11, 2009 12:42 PM
Add VB version codes,
Public Class ThisAddIn
Dim cb As Office.CommandBar
Dim btn As Office.CommandBarButton 'Declare the button in class level scopePrivate 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 = Truebtn = 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.


