简体   繁体   中英

onclick delegate is triggered only once

I am writing an outlook add-in that adds a menu to outlook. Although I set delegate to an action for the menu it seems like it is being removed after one call to the delegate - one click on the menu item . next time user clicks it is not getting to my delegate. code example:

menuCommand = (Office.CommandBarButton)cmdBarControl.Controls.Add(
Office.MsoControlType.msoControlButton, missing, missing, missing, true);

menuCommand.Caption = "&Generate weekly...";
menuCommand.Tag = "Generate";
menuCommand.FaceId = 65;

menuCommand.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(
menuCommand_Generate_Click);

menuCommand = (Office.CommandBarButton)cmdBarControl.Controls.Add(
    Office.MsoControlType.msoControlButton, missing, missing, missing, true);

menuCommand.Caption = "&About";
menuCommand.Tag = "About";
menuCommand.FaceId = 65;

menuCommand.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(
menuCommand_About_Click);
menuCommand.BeginGroup = true;

What should I do in order for the menu to trigger the action every time?

Is menuitem a local variable? , becouse then the garbage collector might clean in up once it falls out of scope.

Try keeping the variable in a global object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM