[英]C# - Dynamically add and remove items from ContextMenuStrip and “Smart Events”
In short my problem is how to implement "Send to" (right click on file on windows) 简而言之,我的问题是如何实现“发送到”(在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: 所以我想要:
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) 在“复制到”子菜单下添加带有“桌面”和所有可移动驱动程序的列表(并删除用户弹出它们的可移动驱动程序)
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... 正如我说的那样,我想将文件复制到可移动驱动程序中,那么,如何添加“动态事件”-我的意思是-如果用户插入4 Disk On Key驱动程序,则在“复制到”子菜单下有新的4行(假设是Desktop和E:\\,F:\\,G:\\,L :),所以我需要为每个可移动驱动程序添加新的click事件,以将文件复制到真正的驱动程序...
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. 关于问题1-我找到了检测可移动驱动程序是否插入计算机的代码,并成功将可移动驱动程序添加到子菜单中。 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 ! 谢谢您的帮助!
Why don't just explicitly remove all menu subitems except the top one, that is "Desktop": 为什么不只显式删除除最上面的菜单项“ 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;
...
Loop through that item's items 循环浏览该项目的项目
ToolStripMenuItem copyToItem = menuStrip.Item(...) ToolStripMenuItem copyToItem = menuStrip.Item(...)
foreach (ToolStripMenuItem item in copyToItem.Items) { copyToItem.Items.Remove(item); foreach(copyToItem.Items中的ToolStripMenuItem项目){copyToItem.Items.Remove(item); }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.