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.
Comments
Anonymous
June 22, 2004
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.VidyaramanAnonymous
June 22, 2004
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.Anonymous
June 23, 2004
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.VidyaramanAnonymous
July 02, 2004
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. ...Anonymous
June 18, 2009
VS Addin: Fast Add Reference Dialog - No more Coffee Break, Steve!Anonymous
June 18, 2009
The comment has been removedAnonymous
June 18, 2009
PingBack from http://adirondackchairshub.info/story.php?id=3576Anonymous
June 19, 2009
VS Addin: Fast Add Reference Dialog - No more Coffee Break, Steve!