3

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 !

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
AsfK
  • 3,328
  • 4
  • 36
  • 73
  • Every time the context menu is opened you should remove all items under the Copy To item and then rebuild the list. This way the items are current. Command: contextMenu.Items.Remove(item); – andrewb Jul 29 '13 at 05:45
  • Thank you, but how can i know how much items I have (in the "Copy to" sub menu) ? – AsfK Jul 29 '13 at 05:46
  • @AsfK you may want to consider `DriveInfo` to look for the `Removable drives`. – King King Jul 29 '13 at 05:49
  • andrew and King - Really thank you very much !! Now I need to know how can I add "click event" for each remoable drive – AsfK Jul 29 '13 at 05:55
  • See http://stackoverflow.com/questions/11121284/how-to-create-a-dynamically-built-context-menu-clickevent - basically in the loop of the removable drives you will do something like: newItem.Click += new Event Handler (VS will auto generate it if you let it) – andrewb Jul 29 '13 at 06:04

2 Answers2

3

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;
    ...
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0
  1. Get reference to the "Copy To" item
  2. Loop through that item's items

    ToolStripMenuItem copyToItem = menuStrip.Item(...)

    foreach (ToolStripMenuItem item in copyToItem.Items) { copyToItem.Items.Remove(item); }

andrewb
  • 2,995
  • 7
  • 54
  • 95
  • Thank you, it's really solve the first question but I still need to know how to detect user click on each removable drive – AsfK Jul 29 '13 at 06:10
  • See my comment above - I provided a link to a SO question. Basically in your code which adds each drive type: menuItem.Click += and Visual Studio will suggest and create a click event for that menu item. Alternatively you can create an event handler and assign that to menuItem.Click – andrewb Jul 29 '13 at 06:13