在form1的顶部:
private ContextMenuStrip contextmenustrip1 = new ContextMenuStrip();
然后:
private void listView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
ListView listView = sender as ListView;
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
ListViewItem item = listView.GetItemAt(e.X, e.Y);
if (item != null)
{
item.Selected = true;
contextmenustrip1.Show(listView, e.Location);
}
}
ContextMenu contextMenu = new System.Windows.Forms.ContextMenu();
MenuItem menuItem = new MenuItem("Cut");
menuItem.Click += new EventHandler(CutAction);
contextMenu.MenuItems.Add(menuItem);
menuItem = new MenuItem("Copy");
menuItem.Click += new EventHandler(CopyAction);
contextMenu.MenuItems.Add(menuItem);
menuItem = new MenuItem("Paste");
menuItem.Click += new EventHandler(PasteAction);
contextMenu.MenuItems.Add(menuItem);
}
}
当我使用断点点击项目时,它会进入事件,但是鼠标右键单击不起作用,但是肯定它没有显示切割复制粘贴的菜单。
我想在右键单击listView中的项目时,它会显示当前项目的菜单。不确定我是先应该首先制作mousedown还是其他一些不确定更多逻辑的事件。但想法是通过鼠标右键单击项目来显示菜单。