VSTO Outlook 2007 Add-in Context Menu CommandBarButton click event
I need to add a button to the context menu of the inbox. I have this working fine. What I need to figure out is in the implementation of the event handler how do I determine which item/items was clicked?
private void AddIn_Startup(object sender, EventArgs e)
{
Application.ItemContextMenuDisplay += Application_ItemContextMenuDisplay;
}
private void Application_ItemContextMenuDisplay(CommandBar commandBar,开发者_开发技巧 Selection selection)
{
commandBar.Controls[1].BeginGroup = true; // add seperator before first menu
var cmdButtonCopy = (CommandBarButton)commandBar.Controls.Add(MsoControlType.msoControlButton, 1, Missing.Value, 1, Missing.Value);
cmdButtonCopy.Caption = "&Copy Message";
cmdButtonCopy.Click += cmdButtonCopy_Click;
}
private void cmdButtonCopy_Click(CommandBarButton ctrl, ref bool canceldefault)
{
// no sender/event args to determine which item was clicked ...
}
In the cmdButtonCopy_Click event handler I need to copy the specific item that was right-clicked but I can't figure out how to tell which item was clicked.
You can use tag property inside the CommandBarButton
I have written the code to solve your issue have a look at it:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Application.ItemContextMenuDisplay += new ApplicationEvents_11_ItemContextMenuDisplayEventHandler(Application_ItemContextMenuDisplay);
}
void Application_ItemContextMenuDisplay(CommandBar CommandBar, Selection Selection)
{
CommandBarButton mycmdbarbtn = (CommandBarButton)CommandBar.Controls.Add(MsoControlType.msoControlButton,missing, missing, 1,true);
mycmdbarbtn.Caption = "Test Button";
mycmdbarbtn.Click += new _CommandBarButtonEvents_ClickEventHandler(mycmdbarbtn_Click);
mailitm=Selection.Application.ActiveExplorer().Selection[1]; // to get the currently selected mailitem.
}
void mycmdbarbtn_Click(CommandBarButton Ctrl, ref bool CancelDefault)
{
MessageBox.Show("The subject of the clicked mail is " + mailitm.Subject);
}
精彩评论