Welcome to MSDN Blogs Sign in | Join | Help

Mark Bower

Random thoughts on Collaborative Apps, Social Software and SharePoint in the Enterprise

News

  • These postings are provided "AS IS" with no warranties, and confer no rights.
    Locations of visitors to this page
Help! My CommandBarButton events stop firing

This is perhaps the most frequently asked question I hear when writing managed COM Addins or Smart Docs.  You’ve wired up an event handler to catch button clicks of custom menu items.  They work for the first few clicks but then mysteriously stop working.  What’s going on?

The answer is almost always that the CommandBarButton objects have been scoped at the method level.  What typically happens is that some indeterminate time after these objects go out of scope, the garbage collector runs and cleans up these objects along with the event hooks you have wired into.

The solution is to make sure you scope these objects as class-level variables so that they stick around until the class gets destroyed.

Posted: Tuesday, June 22, 2004 1:50 PM by bowerm
Filed under: ,

Comments

S.Vidyaraman said:

Thanks ! That helps a lot. I have one other related isssue. Please let me know if there is a better place to ask these questions ...... ( I already tried the NewGroups )

If I wanted to tag an additional event to ALL the CommandBarButtons (say in MS Word), is it OK for me to declare just one class level scope button variable and use it over and over again to add an event handler to all the buttons ? This code seems to work on my developement platform, but if I try it on a new machine (deployment machine), it fails. I initially thought it was a problem with the Office 2003 PIAs, but the add-in loads and performs ALL other functions as expected.
S.Vidyaraman
# June 22, 2004 2:13 PM

Mark Bower said:

You should add a variable for each button, for basically the same reason. Each time you change your variable to reference a different CommandBarButton object you are causing the original reference to go out of scope.
# June 22, 2004 2:47 PM

S.Vidyaraman said:

Thanks again. Are we going to see any posts on this blog relating to IRM in Office 2003 ? or is there a better place to look for IRM related discussions ?
S.Vidyaraman
# June 23, 2004 4:14 PM

Chris Kunicki said:

Mark Bower Tuesday posted a simple tip for a very very very common question:This is perhaps the most frequently asked question I hear when writing managed COM Addins or Smart Docs.  You’ve wired up an event handler to catch button clicks of custom menu items.  They work for the first few clicks but then mysteriously stop working.  What’s going on?The answer is almost always that the CommandBarButton objects have been scoped at the method level.  What typically happens is that some indeterminate time after these objects go out of scope, the garbage collector runs and cleans up these objects along with the event hooks you have wired into.The solution is to make sure you scope these objects as class-level variables so that they stick around until the class gets destroyed. ...
# July 2, 2004 10:25 AM

Weak Reference said:

VS Addin: Fast Add Reference Dialog - No more Coffee Break, Steve!

# June 18, 2009 4:28 PM

Tuna Toksoz said:

I have spent a couple of days on implementing a fast add reference dialog box for Visual Studio. The

# June 18, 2009 4:33 PM

Weak Reference said:

VS Addin: Fast Add Reference Dialog - No more Coffee Break, Steve!

# June 19, 2009 8:19 AM
New Comments to this post are disabled
Page view tracker