winforms listview列标题双击事件

gkn4icbw  于 2023-04-21  发布在  其他
关注(0)|答案(2)|浏览(196)

是否可以捕获列表视图列标题上的鼠标双击事件?
我使用listView1_ColumnClick事件处理了单击事件,但似乎找不到任何方法来捕获双击事件。

suzh9iv8

suzh9iv81#

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

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;
    }
d8tt03nd

d8tt03nd2#

也许(DateTime.Now - lastClickDate).毫秒代替(DateTime.Now - lastClickDate).毫秒会更好。
除了前面的答案,我建议添加鼠标指针的位置。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;
}

相关问题