VSTO Outlook AddIn - Toolbar button Event Handler Fires One Time Only

J

Joseph Geretz

I'm porting a C# Outlook Addin originally engineered as a COM Addin over to
use VSTO. I've gotten this to the point where my VSTO Addin installs its
Menu items and Toolbar buttons when Outlook launches. I've wired up my event
handler to each Menu item and toolbar button. (I use the same Event handler
and I use the Tag property which is different for every Menu Item and
Toolbar buton to determine which menu or button is being clicked and to take
appropriate action. This worked fine for me previously.)

However I'm seeing a very strange behavior: My event handler is fired for
the first toolbar or menu click. But then it never fires thereafter. It's
like after it fires once, it gets 'unhooked' form the event its supposed to
be handling. A breakpoint set in this handler isn't tripped.

Here's the code which wires up the Event handler:

tbButton.Click += new
Office._CommandBarButtonEvents_ClickEventHandler(this.User_Click);
MenuItem.Click += new
Office._CommandBarButtonEvents_ClickEventHandler(this.User_Click);

Here's the Event handler which fires once:

private void User_Click(Office.CommandBarButton cmdButton, ref bool cancel)
{
MessageBox.Show("Hello!");
}

Have you seen anything like this before? Is there something I need to do to
'reset' the Event handler after it completes? I'm completely puzzled. Bear
in mind that this was working beautifully when running as a COM Addin.

Thanks for your help,

- Joseph Geretz -
 
M

magne

Hello Joseph
Please have a look at my thread from today 12:00 (local time), subject "VSTO
2005 problems".
I wonder if we have the same problem.
I found a weired "solution" by adding a gc collect just after the event
handler wireup logic (...Click += ...), could you try something similar and
see if it has effect ?
 
J

Joseph Geretz

Thanks Harry - that's exactly it!

(Heh - this former ActiveX developer just survived his first collision with
the .NET Garbage Collector! ;-)

Explain one thing to me, though. I had assumed that Outlook's reference to
this button (after all the button does remain on Outlook's toolbar) would be
enough to keep this object around. Isn't my event handler set to handle
events from a button created and maintained by Outlook (on my behalf of
course). Why is this insufficient?

It's also very interesting, that my previous implementation of this project,
which used Extensibility (Extensibility.IDTExtensibility2) rather than VSTO
was not succeptible to GC in this regard. I only started losing my events
when I converted my implementation to use VSTO.

Thanks for your help! Can you help me with my Property Page problem (post
directly above this one)?

Thanks!

- Joe Geretz -
 
G

Galcho[MCSD.NET]

how do you build menu items and buttons?
Please be sure you declared all object in class not in method
boundaries.

there is huge possibility to be GC when you leave method boundaries

hope this helps
Galin Iliev[MCSD.NET]
www.galcho.com
 
J

Joseph Geretz

OK, thanks. I've posted the question to the forum you suggest.

BTW, is this newsgroup for VSTO, or some other topic? I understood
vstools.office to indicate that this is the forum for VSTO.

Thanks,

- Joe Geretz -
 

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