My application has a menu at the top.
The first item in the menu (File) has the usual New, Open..., Save, Save As... as well as Open Recent. This last one is a ToolStripDropDown showing a most-recently-used list of filenames.
I would like to add a right-click Context Menu (not a sub-menu) to the file names which will have one item in the context menu, to remove the right-clicked filename from the list.
I load the file names into the Menu as follows:
private void mnuFile_DropDownOpened(object sender, EventArgs e)
{
foreach (string fn in mru_files)
{
ToolStripMenuItem p = new ToolStripMenuItem(fn);
p.Click += fn_clicked;
p.MouseDown += fn_MouseDown;
openRecentToolStripMenuItem.DropDownItems.Add(p);
}
}
The main portion of this works fine - the fn_clicked
method is invoked when I click on a file and it will do what it's supposed to do.
In the MouseDown
Handler I can remove the file from the list as follows:
private void fn_MouseDown(object sender, MouseEventArgs e)
{
ToolStripMenuItem toolStripMenuItem = sender as ToolStripMenuItem;
if (toolStripMenuItem != null
&& e.Button == System.Windows.Forms.MouseButtons.Right
&& mru_files.Find(x => x == toolStripMenuItem.Text) != null)
{
mru_files.Remove(toolStripMenuItem.Text);
}
}
but this doesn't show a menu.
If I add a context menu to the form, and do
mnu_ctxMRUitem.Show(xyz, e.X, e.Y);
instead of removing the file, I get the Context Menu in the right place, but the original menu with the list of files has disappeared.
How can I show the context menu on right-click of a menu item, without the main menu disappearing.