You are here: Plugin API > Samples > How To Add And Remove A Context Menu Item

.

How To Add And Remove A Context Menu Item

Here is an example of how to add and properly remove a context menu item for a given instance of IDocument.

public void AddContextMenuItem(IDocument doc)

{

if (doc != null)

{

List<System.Windows.Forms.ToolStripItem> cms = doc.GetUserPendingContextMenuItems();

cms.Add(new System.Windows.Forms.ToolStripMenuItem("Editor Plugin Item"));

}

}

public void RemoveContextMenuItem(IDocument doc)

{

if (doc != null)

{

List<System.Windows.Forms.ToolStripItem> cms = mCurrentDocument.GetUserPendingContextMenuItems();

cms.Clear();

}

}

Note: If you have multiple documents with custom context menu items, you will need to manually clear their context toolstripitem lists.