msoControlButton loses Click event delegate after 2 clicks - VS Add-in

M

Max Khitrov

Hello everyone,

I ran into a small problem here and just can't figure it out. I'm
writing a VS .NET add-in which creates a new commandbar and populates it
with a few buttons. Each button is assigned a delegate for the Click
event, but what seems to happen is that it works the first 2 times, and
then the button does nothing after that. The delegate doesn't appear to
be called anymore. Below are a few parts of the code related to this issue.

// First we create a new commandbar
myBar = applicationObject.CommandBars.Add("some
bar",msoBarPosition.msoBarTop, false, true);

// Now lets add one button
CommandBarButton button =
(CommandBarButton)myBar.Controls.Add(MsoControlType.msoControlButton, 1,
"", 1, true);

// And setup its click delegate
button.Click += new
_CommandBarButtonEvents_ClickEventHandler(ButtonClicked);

// Here's the ButtonClicked method
public void ButtonClicked(CommandBarButton button, ref bool clicked)
{
System.Windows.Forms.MessageBox.Show("click");
}

Obviously these are all in different parts of the program, but I doubt
that's relevant. When the add-in loads, I add the commandbar to the top
and click the button 2 times. The first time I get the messagebox, the
second time I get the message box. After that, no matter how many times
I click the button, the ButtonClicked method is never called. The same
thing also happens if I put a breakpoint right on the MessageBox.Show
line. The first time this line is executed, the messagebox shows up, I
click OK, then I use F11 to get to the end of that method. Once back in
the VS, the button doesn’t work.

Any ideas on what I'm doing wrong? Thanks for any help.
 
W

wackyphill

Are you storing your references in your addin class as members? Make
sure they are not local variables. Remember the GC can destory your
objects unless you keep references to everything.

Check that nothing is being garbage collected. That'd be my 1st guess.
 
M

Max Khitrov

Are you storing your references in your addin class as members? Make
sure they are not local variables. Remember the GC can destory your
objects unless you keep references to everything.

Check that nothing is being garbage collected. That'd be my 1st guess.

Hm... I think you may be right, I just created an array to store all
buttons after they are created and everything continues to work fine.
I'm not sure I quite understand why though... My guess is that it's the
delegate object that is destroyed, but shouldn't GC see that it's being
referenced by the Click event? And even so, I don't see how storing the
actual button after it is created helps in preventing the delegate from
being destroyed. That is unless it's the button object that is
destroyed, but that just doesn’t make sense how I could have a button on
the commandbar without it actually being an instance of the
commandbarbutton.

Strange... Anyway, thanks for the tip.
 
W

wackyphill

The button (In respect to .net) was being GC'd (And the bar probably
too). The button contains the event. The Event is essentially a
collection of delegates to functions.

Kill the button, you kill everything it contains. This kills your
delegate.
 

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