我想检测 winforms 列表框控件中双击的项目。 [如何处理点击空白区域?]
好吧,我有一个列表框,里面有一些项目。
我想检测双击某个项目。
目前我使用的方法有一个问题,如果用户双击空白处,当前选定的项目将被标记为双击。
更新:
请注意,这个问题并不像乍看起来那么简单。
另请注意,Timwi 答案不正确,因为如果选择了一个项目并且我在空白处单击,则 [if (ListBox1.SelectedIndex == -1)] 部分不会执行 我不知道谁给他投票,但他的回答不正确。
我已经写好了这部分代码
如果有一个函数可以将鼠标坐标转换为列表框项目,那么问题将得到解决
well i have a listbox with some items inside.
i want to detect a double click on an item.
currently the method i am using have a problem that if a user double click on an empty spot the currently selected item is signaled as double clicked.
Update:
Please note that this question is not as easy as it seems at first.
also note that Timwi answer is not correct because the [if (ListBox1.SelectedIndex == -1)] part dont get executed if there is an item selected and i clicked in an empty space
i dont know who upvoted him but his answer is not correct.
i already had this part of code written
if there is a function that can convert mouse coordinates to a listbox item then the problem will be fixed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
还有一个替代事件:
MouseDoubleClick
,它提供了MouseEventArgs,因此您可以获得点击坐标。然后,您可以调用GetItemBounds()
获取包含所选项目的矩形,并检查鼠标坐标是否在此矩形内:MouseDoubleClick
版本信息:There is an alternative event:
MouseDoubleClick
, which provides MouseEventArgs, so you can get click coordinates. Then you can callGetItemBounds()
to get rectangle, containing selected item and check if mouse coordinates are within this rectangle:MouseDoubleClick
Version Information:这是我用于单个 MouseClick 的内容,可能会进行调整。
首先,我将 CheckListBox.CheckOnClick 的属性设置为 true:
clb.CheckOnClick = true;
然后我在框检查状态更改后强制取消选择该项目:
Here's what I used for a single MouseClick, might be adapted.
First I set the properties of the CheckListBox.CheckOnClick to true:
clb.CheckOnClick = true;
then I forced the item to deselect after the box checkstate changed: