的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:\Folder1\Test.pdf");
MessageBox.Show("A test");
}
}
请注意:当子项 1 列表视图,文件打开,但至少出现两次消息框。但是,当我注释掉打开只出现一次的消息框中的文件中的行(因为它应该)。我需要打开其名称由ListView中,用户点击该文件。我怎样才能做到这一点,而不MoueUp事件触发多次吗?
另请注意,列表视图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屋!