gpt4 book ai didi

C# - 从 ContextMenuStrip 和 "Smart Events"动态添加和删除项目

转载 作者:行者123 更新时间:2023-12-05 04:18:07 25 4
gpt4 key购买 nike

简而言之,我的问题是如何实现“发送到”(在 Windows 上右键单击文件)

我有一个数据 GridView ,其中还包含一个带有日志文件名的列(我知道每个文件的路径)

我想在我的弹出菜单中添加复制选项到桌面和磁盘上的关键(可移动)驱动程序。

我的弹出菜单可能是这样的:

   View log

Open file location

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

Copy to --> Desktop
(and Removable Drivers)

...

所以我想:

  1. 在“复制到”子菜单下添加包含“桌面”和所有可移动驱动程序的列表(并删除用户弹出的可移动驱动程序)

  2. 正如我所说,我想将文件复制到可移动驱动程序,那么我如何添加“动态事件”——我的意思是——如果用户插入 4 Disk On Key 驱动程序,我在“下有新的 4 行”复制到”子菜单(比方说,桌面和 E:\、F:\、G:\、L:),所以我需要为每个可移动驱动程序创建新的单击事件,以将文件复制到真正的驱动程序...

关于问题 1 - 我找到了检测可移动驱动程序是否插入计算机的代码,并且我成功地将可移动驱动程序添加到子菜单中。但是我没有从子菜单中删除项目:

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 + ")");
}
}
}

谢谢你的帮助!

最佳答案

为什么不明确删除除顶部菜单项(即“桌面”)之外的所有菜单子(monad)项:

  ...
// 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;
...

关于C# - 从 ContextMenuStrip 和 "Smart Events"动态添加和删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17916710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com