【发布时间】:2012-06-24 09:51:49
【问题描述】:
当文件显示在 datagridview 中时,我会在单击该特定文件夹名称时列出目录中的文件。现在使用上下文菜单,我想在该上下文菜单中添加此 sendto 选项,并希望将该文件发送到任何可移动媒体。
【问题讨论】:
当文件显示在 datagridview 中时,我会在单击该特定文件夹名称时列出目录中的文件。现在使用上下文菜单,我想在该上下文菜单中添加此 sendto 选项,并希望将该文件发送到任何可移动媒体。
【问题讨论】:
您在 Windows 的“发送至”菜单中看到的程序快捷方式存储在 %APPDATA%\Microsoft\Windows\SendTo 文件夹中。
阅读此文件夹的内容并在网格的上下文菜单中显示选项。
快捷方式是.LNK 文件。从LNK文件中解析EXE的名字,调用EXE使用System.Diagnostics.Process.Run
以下是如何从 LNK 文件中解析 EXE 位置
【讨论】:
尝试修改这个例子,它在不同的列上启用不同的选项:
//Define different context menus for different columns
private ContextMenu contextMenuForColumn1 = new ContextMenu();
private ContextMenu contextMenuForColumn2 = new ContextMenu();
Add the following line of code in the form load event:
private void Form_Load(object sender, EventArgs e)
{
// Load all default values of controls
populateDataGridView();
// Add context mneu items
contextMenuForColumn1.MenuItems.Add("Make Active", new EventHandler(MakeActive));
contextMenuForColumn2.MenuItems.Add("Delete", new EventHandler(Delete));
contextMenuForColumn2.MenuItems.Add("Register", new EventHandler(Register));
}
Add the following code to mouseup event of the gridview:
private void dataGridView_MouseUp(object sender, MouseEventArgs e)
{
// Load context menu on right mouse click
DataGridView.HitTestInfo hitTestInfo;
if (e.Button == MouseButtons.Right)
{
hitTestInfo = dataGridView.HitTest(e.X, e.Y);
// If column is first column
if (hitTestInfo.Type == DataGridViewHitTestType.Cell && hitTestInfo.ColumnIndex == 0)
contextMenuForColumn1.Show(dataGridView, new Point(e.X, e.Y));
// If column is second column
if (hitTestInfo.Type == DataGridViewHitTestType.Cell && hitTestInfo.ColumnIndex == 1)
contextMenuForColumn2.Show(dataGridView, new Point(e.X, e.Y));
}
}
关于 SO 的类似问题:
【讨论】: