gpt4 book ai didi

c# - 在 C# 中处理 ListBox 项上的双击事件

转载 作者:太空狗 更新时间:2023-10-29 22:55:36 26 4
gpt4 key购买 nike

我想在双击列表框中的项目时执行某些操作。我找到了这样做的代码

void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
int index = this.listBox1.IndexFromPoint(e.Location);
if (index != System.Windows.Forms.ListBox.NoMatches)
{
MessageBox.Show(index.ToString());
//do your stuff here
}
}

但是,当我单击某个项目时,不会触发该事件。如果我单击所有项目下方的列表框,就会触发该事件。

我设置了 ListBox 的 DataSource 属性至 IList<MyObject> .

有什么想法吗?

最佳答案

尝试使用带有 MouseDown 和 DoubleClick 事件的 ListBox 创建表单。据我所知,DoubleClick 不会触发的唯一情况是在 MouseDown 内部调用 MessageBox.Show(...)。在其他情况下它工作正常。

还有一件事,我不确定它是否重要,但无论如何。当然也可以这样获取item的索引:

int index = this.listBox1.IndexFromPoint(e.Location);

但是这种方式也可以:

if (listBox1.SelectedItem != null)
...

关于c# - 在 C# 中处理 ListBox 项上的双击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3434154/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com