473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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(MsoControlTyp e.msoControlButton, 1,
"", 1, true);

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

// 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.
Nov 17 '05 #1
3 1962
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.

Nov 17 '05 #2
wa********@yahoo.com wrote:
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.
Nov 17 '05 #3

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.

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Wajih-ur-Rehman | last post by:
I am developing a C# app. I want to do something when someone double clicks on a combo box. But this event never fires. Any suggestions? Thanx!
0
by: msnews.microsoft.com | last post by:
I have a delegate defined as ... public delegate void MessageHandler<T>(T t, object arg) where T : MessageBase, new(); and I am trying to create an event to handle that delegate (shown below)...
4
by: Mark Lingen | last post by:
I've found a problem with postback event handling and webcontrol buttons. Try out the following code in an ASP.Net project and you will see. Create a web project in VB.Net and drop this code...
2
by: harry | last post by:
Hi I am trying to add an event handler to the window so that If the user clicks the close window button, it creates a popup to tell the user it should use the log out button before closing window....
11
by: Bernie Yaeger | last post by:
If the user clicks the processit button, the click event code runs. What happens if he/she clicks it again before it's done processing? Does the system remember and process twice? Does it ignore...
2
by: wink martindale | last post by:
Hello All, We are developing a vb.net app that utilizes a tabbed interface. On one tab page, we have "embedded" a form. Our question is this: How do we respond to the click event on the TAB as...
0
by: scott_gui | last post by:
I tried to post this a few seconds ago and isn't showing. My apologies if it posts twice. I am creating a Windows application: The mouse event <Double-Button-1> has a conflict when the...
0
by: Paul DeWitt | last post by:
I have a textbox on a Windows Form. I am wanting to display a popup form to request/validate info if the user clicks the textbox and have a different form (to delete data) load if the user either...
2
by: bg_ie | last post by:
Hi, I have a treeview where the user selects a node before editing it. The problem is that when the user clicks on the text box to edit the node, the treeview loses its focus and the node is no...
2
by: govolsbaby | last post by:
Hi All I've got a treeview with checkboxes. I want the user to be able to toggle the checkbox by click on the checkbox OR by clicking on the node text. So I've added an afterselect event...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.