C # (add-in add-on) context menu in folders

In my Outlook VSTO addin, I'm trying to put a button that will appear when I right-click on a folder. In my autoload function, I have the following:

Outlook.Application myApp = new Outlook.ApplicationClass(); myApp.FolderContextMenuDisplay += new ApplicationEvents_11_FolderContextMenuDisplayEventHandler(myApp_FolderContextMenuDisplay); 

then I have a handler for this ...

 void myApp_FolderContextMenuDisplay(CommandBar commandBar, MAPIFolder Folder) { var contextButton = commandBar.Controls.Add(MsoControlType.msoControlButton, missing, missing, missing, true) as CommandBarButton; contextButton.Visible = true; contextButton.Caption = "some caption..."; contextButton.Click += new _CommandBarButtonEvents_ClickEventHandler(contextButton_Click); } 

and finally a click handler ....

 void contextButton_Click(CommandBarButton Ctrl, ref bool CancelDefault) { //stuff here } 

My question is how to send this MAPIFolder Folder from myApp_FolderContextMenuDisplay to contextButton_Click ?

(If it can be done differently, I am also open to suggestions)

+6
source share
1 answer

The easiest way is to simply use closure, for example:

 // where Folder is a local variable in scope, such as code in post contextButton.Click += (CommandBarButton ctrl, ref bool cancel) => { DoReallStuff(ctrl, Folder, ref cancel); }; 

Be sure to clear the event if necessary. The only thing you need to pay attention to is that RCW for Folder can now have an "extended lifetime", since closing can support it longer than before (but with OOM it is very important to manually release RCW if it is not required).

Happy coding.

+3
source

Source: https://habr.com/ru/post/885661/


All Articles