[英]How to open a context menu with right click on a ListViewItem
The title pretty much says it all. 标题基本概括了所有内容。 Can someone explain to me how can I open a context menu by selecting and then right clicking on a ListViewItem
of a ListView
? 有人可以向我解释如何通过选择然后右键单击ListViewItem
的ListView
来打开上下文菜单吗?
I tried using the following code 我尝试使用以下代码
private void listView1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (listView1.FocusedItem.Bounds.Contains(e.Location) == true)
{
contextMenuStrip1.Show(Cursor.Position);
}
}
}
But I don't know how to register this event handler with the ListView
. 但是我不知道如何向ListView
注册此事件处理程序。 Every time I try I get the error that the delegates parameters are wrong because I use MouseEventArgs
instead of EventArgs
. 每次尝试时,我都会收到错误的委托参数,因为我使用MouseEventArgs
而不是EventArgs
。
This is the wrong code I'm using to register the EventHandler
这是我用来注册EventHandler
的错误代码
this.listView1.MouseClick += new System.EventHandler(this.listView1_MouseClick);
bind the contextmenu to the listview using listView1.ContextMenu=contexMenu1
使用listView1.ContextMenu=contexMenu1
将contextmenu绑定到listview
then you can use the below code in listView1.MouseDown
那么您可以在listView1.MouseDown
使用以下代码
ListViewHitTestInfo lstHitTestInfo = listView1.HitTest(e.X, e.Y);
if (e.Button == MouseButtons.Right)
{
if (lstHitTestInfo.Item != null)
{
listView1.ContextMenuStrip = contextMenuStrip1;
}
}
this will select the listviewitem and display the contextmenu. 这将选择listviewitem并显示上下文菜单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.