C# - Dynamically add and remove items from ContextMenuStrip and "Smart Events"

AsfK picture AsfK · Jul 29, 2013 · Viewed 8.7k times · Source

In short my problem is how to implement "Send to" (right click on file on windows)

I have a data grid view that also contain a column with log file name (I know the path for each file)

I want to add to my pop-up menu Copy options to Desktop and Disk on Key (removable) drivers.

My pop-up menu might look like this:

   View log

   Open file location

   <---------------->

   Copy to -->  Desktop
                (and Removable Drivers)

   ...

So I want:

  1. to add a list with "Desktop" and all removable drivers under "Copy to" sub menu (and to remove removable drivers that the user eject them)

  2. As I say I want to copy the file to the removable driver, So How can I add "dynamically event" - I meaning - if the user plug in 4 Disk On Key drivers I have new 4 lines under "Copy to" sub menu (Let's say, Desktop and E:\, F:\, G:\, L:), So I need new click event for each Removable driver to copy the file to the true driver...

About Question 1 - I found the code that detect if removable driver plug in the computer and I success to add the removalble drivers to the sub menu. But I unsuccessful in removing the items from the sub menu:

private void menu_PopUp_Opening(object sender, CancelEventArgs e)
{
    // Need to remove all removable drivers first --> How to do ?

    // to update the USB drivers when opening new pop up menu
    DriveInfo[] ListDrives = DriveInfo.GetDrives();
    foreach (DriveInfo Drive in ListDrives)
    {
        if (Drive.DriveType == DriveType.Removable)
        {
            // add to popup menu, from: http://stackoverflow.com/questions/5868446/how-to-add-sub-menu-items-in-contextmenustrip-using-c4-0
            (menu_PopUp.Items[3] as ToolStripMenuItem).DropDownItems.Add(Drive.Name + " (" + Drive.VolumeLabel + ")");
        }
    }
}

Thank you any help !

Answer

Dmitry Bychenko picture Dmitry Bychenko · Jul 29, 2013

Why don't just explicitly remove all menu subitems except the top one, that is "Desktop":

  ...
  // Need to remove all removable drivers first
  ToolStripMenuItem copyToItem = menu_PopUp.Items[3] as ToolStripMenuItem;

  // Assuming that "Desktop" menu item is the top one, 
  // we should delete all the items except #0 
  for (int i = copyToItem.DropDownItems.Count - 1; i >= 1; --i)
    copyToItem.DropDownItems.RemoveAt(i);

  ...
  // to update the USB drivers when opening new pop up menu
  DriveInfo[] ListDrives = DriveInfo.GetDrives();

  foreach (DriveInfo Drive in ListDrives) {
    if (Drive.DriveType == DriveType.Removable) {
      // add to popup menu, from: http://stackoverflow.com/questions/5868446/how-to-add-sub-menu-items-in-contextmenustrip-using-c4-0
      ToolStripItem item = (menu_PopUp.Items[3] as ToolStripMenuItem).DropDownItems.Add(Drive.Name + " (" + Drive.VolumeLabel + ")");

      item.Tag = Drive.Name; // <- bind (via tag) driver name with menu item
      item.Click += OnRemovableDriveClick;
    }
  }

...

  private void OnRemovableDriveClick(object sender, EventArgs e) {
    ToolStripItem item = sender as ToolStripItem;

    String driveName = item.Tag as String;
    ...