如何用c#右键选择一个ListView项目



我创建了一个ListView显示图像。我添加了一个MouseDown事件处理程序,在里面我做了:

private void FooListView_MouseDown(object sender, MouseEventArgs e)         
{
if (e.Button == MouseButtons.Right)
{
var focusedItem = ltView.FocusedItem;
if (focusedItem != null && focusedItem.Bounds.Contains(e.Location))
cmsIconMenu.Show(FooListView, e.Location);
else
MessageBox.Show("Vous n'avez sélectionné(e) aucune icône.");
}
}

,因为它讨厌左击,然后右键单击直接保存图像。其他应用程序有这个问题。因此,我需要添加一些代码来检查是否:用户右键单击ListView项,关注它并显示上下文菜单。

我试过:检查一个帖子,我丢失了链接,但它没有做的工作。

(问我是否对我的问题不够详细)。

(它是法语,因为它是我的应用程序的法语版本)

好的,那么答案就是Jimi的答案。这里的代码是

private void FooListView_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var lv = sender as ListView; // [...]
var item = lv.HitTest(e.Location).Item;

if (item == null)
{
MessageBox.Show("Vous n'avez sélectionné(e) aucune icône."); //In English it's You haven't selected any icons (images).
}
else
{
lv.FocusedItem = item;
cmsIconMenu.Show(lv, e.Location);
}
}
}

谢谢吉米!

你也可以用另一个名字替换方法的名字,但是保留这些参数(parameters)!

最新更新