右键单击以选择列表框中的项目 [英] Right Click to select items in a ListBox
问题描述
我正在尝试列出一些项目,您可以通过右键单击并显示上下文菜单来执行多项操作.我已经完成了,没问题.
I'm trying to make a list of items that you can do several actions with by right-clicking and having a context menu come up. I've completed that, no problem whatsoever.
但是我想拥有它,以便当您右键单击某个项目时,而不是使当前项目保持选中状态,而是选择鼠标悬停在该项目上.
But I'd like to have it so that when you right click on a item, instead of leaving the current item selected, to select the item the mouse is over.
我已经研究了这个问题以及其他相关问题,并且尝试使用indexFromPoint(通过研究发现),但是每当我右键单击某个项目时,它始终只会清除选定的项目而不会显示我已对其进行了设置,以便在没有选定项目的情况下不会显示它.
I've researched this and other related questions, and I've tried to use indexFromPoint (which I found through my research) but whenever I right click on a item, it always just clears the selected item and doesn't show the context menu, as I have it set so that it wont appear if there is no selected item.
这是我当前正在使用的代码:
Here is the code I'm currently using:
ListBox.SelectedIndex = ListBox.IndexFromPoint(Cursor.Position.X, Cursor.Position.Y);
推荐答案
处理ListBox.MouseDown
并选择其中的项目.像这样:
Handle ListBox.MouseDown
and select the item in there. Like this:
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
listBox1.SelectedIndex = listBox1.IndexFromPoint(e.X, e.Y);
}
这篇关于右键单击以选择列表框中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!