listview列标题双击事件

时间:2017-12-14 07:37:51

标签: c# forms winforms

是否可以在listview列标题上捕获鼠标双击事件?
我使用listView1_ColumnClick事件处理了点击事件,但我似乎无法找到任何方法来点击它。 enter image description here

2 个答案:

答案 0 :(得分:0)

如果您想管理一个没有双击事件的控件,您可以使用时间戳来计算点击之间的时差。喜欢:

    DateTime lastClickDate = DateTime.Now;
    private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
    {
        //You can change the value 1200 as you want.
        if ((DateTime.Now - lastClickDate).TotalMilliseconds < 1200)
        {
            MessageBox.Show("double clicked");
        }
        this.Text = (DateTime.Now - lastClickDate).TotalMilliseconds.ToString();
        lastClickDate = DateTime.Now;
    }

答案 1 :(得分:0)

除了上一个答案,我建议添加鼠标指针的位置。 Cursor.Position在这方面会有所帮助。

DateTime lastClickDate = DateTime.Now;
int xPosition = Cursor.Position.X;
int yPosition = Cursor.Position.Y;  
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
    //You can change the value 1200 as you want.
    if ((DateTime.Now - lastClickDate).TotalMilliseconds < 1200)
    {
        //Just for example I have given as equals it is better to allow
        //some variation since mouse may be moved a little during clicks          
        if(Cursor.Position.X==xPosition && Cursor.Position.Y==yPosition)
        {   
            MessageBox.Show("double clicked");
        } 
    }
    this.Text = (DateTime.Now - lastClickDate).TotalMilliseconds.ToString();
    lastClickDate = DateTime.Now;
}