Winforms ListView MouseUp 事件触发不止一次 [英] Winforms ListView MouseUp event firing more than once
问题描述
在我的 .NET 4,5 Winforms 应用程序中,当我从该事件中打开文件时,ListView 控件的 MouseUp 事件多次触发,如下所示:
In my .NET 4,5 Winforms application, the ListView control's MouseUp event is firing multiple times when I open a file from that event as follows:
private void ListView1_MouseUp(object sender, MouseEventArgs e)
{
ListViewHitTestInfo hit = ListView1.HitTest(e.Location);
if (hit.SubItem != null && hit.SubItem == hit.Item.SubItems[1])
{
System.Diagnostics.Process.Start(@"C:Folder1Test.pdf");
MessageBox.Show("A test");
}
}
注意:当点击 SubItem1 时在列表视图中,文件打开但消息框至少出现两次.但是,当我注释掉打开文件的行时,消息框只出现一次(应该如此).我确实需要打开用户在列表视图中单击其名称的文件.如何在没有多次触发 MoueUp 事件的情况下实现这一目标?另请注意,listview 的 MouseClick 事件并不总是像 所述这里.所以,我必须使用 MouseUp 事件.
Note: When clicking on the SubItem1 of the listview, the file opens but the message box appears at least twice. But, when I comment out the line that opens the file the message box appears only once (as it should). I do need to open the file whose name is clicked by the user in the listview. How can I achieve this without the MoueUp event firing multiple times? Please also note that the MouseClick event for listview does not always work as also stated here. So, I have to use MouseUp event.
我应该提到 ListView 处于详细信息模式.
I should mention the ListView is in Details mode.
推荐答案
避免 HitTest()
并使用 ListView
的原生函数 GetItemAt()
代码>.来自 MSDN 的示例如下所示:
Avoid HitTest()
and use ListView
's native function GetItemAt()
. An example from MSDN looks like this:
private void ListView1_MouseDown(object sender, MouseEventArgs e)
{
ListViewItem selection = ListView1.GetItemAt(e.X, e.Y);
// If the user selects an item in the ListView, display
// the image in the PictureBox.
if (selection != null)
{
PictureBox1.Image = System.Drawing.Image.FromFile(
selection.SubItems[1].Text);
}
}
这篇关于Winforms ListView MouseUp 事件触发不止一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!