在C#中的ListView上触发DoubleClick事件

时间:2011-11-22 11:55:58

标签: c# winforms events

是否可以通过编程方式在DoubleClick上触发ListView事件?无需知道事件处理程序的位置/签名?

4 个答案:

答案 0 :(得分:2)

如果我理解了你想要的东西而不是这样做:

private void MouseDoubleClick(object sender, EventArgs e)
{
   //some code on mouse double click
}

请:

private void MethodToExecuteOnDoubleClick()
{
  //some code on mouse double click
}

private void MouseDoubleClick(object sender, EventArgs e)
{
   MethodToExecuteOnDoubleClick();
}

然后你可以随时调用MethodToExecuteOnDoubleClick()而不需要升级doubleclick事件

答案 1 :(得分:1)

对于模拟鼠标点击,您可以执行以下操作:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

  //....

   [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
   public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

   private const int MOUSEEVENTF_LEFTDOWN = 0x02;
   private const int MOUSEEVENTF_LEFTUP = 0x04;
   private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
   private const int MOUSEEVENTF_RIGHTUP = 0x10;


   public void DoMouseClick()
   {
      //Call the imported function with the cursor's current position
      int X = Cursor.Position.X;
      int Y = Cursor.Position.Y;
      mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
   }

   //...
}

答案 2 :(得分:1)

我刚才在博客上写道:Simulate a Click;它不是真正的点击,但它会触发事件处理程序。该博客称“OnClick”,将其替换为“OnDoubleClick”,你应该没问题。

答案 3 :(得分:0)

最好创建一个封装控件并处理那里可能需要的任何逻辑。

相关问题