VSTO: Remove button but event still fires

F

fedebona

I have an outlook toolbar where I dynamically rebuild some buttons from a
popupMenu. To do this I call Delete Method to remove old buttons and recall
Add method to add new ones.
graphically it's all OK: old buttons disappear and new ones are correctly
visible. But Delete method probably doesn't kill the COM object that
represents the button, so the event handler associated with the button is
still alive
I tried remove event handler registration first but it gives me an error:

((Office.CommandBarButton)rootMenu.Controls[index]).Click -=
imAppsMenu_Click;
rootMenu.Controls[index].Delete(true);

The line where I try to deregister the event handler gives me a null
reference error. If I remove it, the event handler is still registered and
when I subsequently add a new button with the same event handler then the
handler is called twice....

So, what's the correct way to remove a button that has a registered event
handler from a toolbar via VSTO?
Thanks
 
K

Ken Slovak - [MVP - Outlook]

Have you tried calling Marshal.ReleaseComObject() on the buttons you want to
remove after you delete them and before you set those button objects ==
null?
 
F

fedebona

Yes, I tried both of your suggestions, but the situation is still the same.
The button seems to disappear, but the handler still fires.
What really disappoints me is the fact that when I try to first remove the
handler to the click event of the button I get an error. But I'm sure that
the handler is registered, so why do I get this error? Is there a different
way to remove event handler from the button?
 
F

fedebona

Yes, I tried both of your suggestions, but the situation is still the same.
The button seems to disappear, but the handler still fires.
What really disappoints me is the fact that when I try to first remove the
handler to the click event of the button I get an error. But I'm sure that
the handler is registered, so why do I get this error? Is there a different
way to remove event handler from the button?
 
K

Ken Slovak - [MVP - Outlook]

The only other thing I can think of would be to actually get the button
object instead of referencing it like:

((Office.CommandBarButton)rootMenu.Controls[index]).Click -=
imAppsMenu_Click;
rootMenu.Controls[index].Delete(true);

I'm wondering if it's an Interop thing with the PIA where even though you're
casting your CommandBarControls[index] object as CommandBarButton whether
it's objecting to removing a click event there since a CommandBarControl
doesn't have a click event.

See if that makes any difference.
 
F

fedebona

Thank you very much for your suggestion!!!
I finally solved the problem simpy changing the way of accessing to my
buttons.
Whenever I create a new Office.CommandBarButton I save it in a
List<Office.CommandBarButton> and when I delete them I don't reference via
container, but simply accessing to my list<>. This way everything worked fine

Ken Slovak - said:
The only other thing I can think of would be to actually get the button
object instead of referencing it like:

((Office.CommandBarButton)rootMenu.Controls[index]).Click -=
imAppsMenu_Click;
rootMenu.Controls[index].Delete(true);

I'm wondering if it's an Interop thing with the PIA where even though you're
casting your CommandBarControls[index] object as CommandBarButton whether
it's objecting to removing a click event there since a CommandBarControl
doesn't have a click event.

See if that makes any difference.




fedebona said:
Yes, I tried both of your suggestions, but the situation is still the
same.
The button seems to disappear, but the handler still fires.
What really disappoints me is the fact that when I try to first remove the
handler to the click event of the button I get an error. But I'm sure that
the handler is registered, so why do I get this error? Is there a
different
way to remove event handler from the button?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top