【问题标题】:how can I handle both winforms listview click and doubleclick events?如何处理 winform 列表视图单击和双击事件?
【发布时间】:2020-07-14 05:38:40
【问题描述】:

我的申请如下

程序.cs

using System;
using System.Windows.Forms;

namespace WindowsFormsClickAndDoubleClick
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
</configuration>

Form1.cs

using System.Windows.Forms;

namespace WindowsFormsClickAndDoubleClick
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

      

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            MessageBox.Show("Double click");
        }

        private void listView1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Single click");
        }
    }
}

Form1.Designer.cs

namespace WindowsFormsClickAndDoubleClick
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem("one");
            System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem("two");
        
            this.listView1 = new System.Windows.Forms.ListView();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            // 
            // listView1
            // 
            this.listView1.HideSelection = false;
            this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
            listViewItem1,
            listViewItem2,
          });
            this.listView1.Location = new System.Drawing.Point(97, 56);
            this.listView1.Name = "listView1";
            this.listView1.Size = new System.Drawing.Size(254, 134);
            this.listView1.TabIndex = 1;
            this.listView1.UseCompatibleStateImageBehavior = false;
            this.listView1.View = System.Windows.Forms.View.List;
            this.listView1.Click += new System.EventHandler(this.listView1_Click);
            this.listView1.DoubleClick += new System.EventHandler(this.listView1_DoubleClick);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.listView1);
            this.Name = "Form1";
            this.Text = "Form1";
          
            this.ResumeLayout(false);

        }

        #endregion
        private System.Windows.Forms.ListView listView1;
        private System.Windows.Forms.Timer timer1;
    }
}

我想同时处理单击和双击事件 然而我只能得到点击事件消息显示。

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    这是因为您在单击时阻止

    也就是说,您在单击(带有MessageBox)时阻止了消息泵(UI),系统将无法识别双击。它超过了它认为下一次单击(在显示对话框时被暂停)作为一部分的时间双击序列。

    改为使用Debug.WriteLine 并在output window 中查看结果

    private void listView1_DoubleClick(object sender, EventArgs e)
    {
        Debug.WriteLine("Double click");
    }
    
    private void listView1_Click(object sender, EventArgs e)
    {
        Debug.WriteLine("Single click");
    }
    

    来自Jakob Busk Sørensen的精彩评论

    这也意味着您可以使用单击的内容有限制 对于,如果您还想使用双击。基本上任何东西 单击运行,也将在双击运行。或者 考虑使用右键单击,具体取决于您要执行的操作。

    【讨论】:

    • 这也意味着如果您还想使用双击,您可以使用单击的内容是有限的。基本上任何通过单击运行的东西也将在双击时运行。或者考虑使用右键单击,具体取决于您要执行的操作。
    • @JakobBuskSørensen 将您的评论添加到答案中并注明出处
    【解决方案2】:

    如果您想同时处理Click 和Double-Click 并执行不同的操作,或者防止一个操作两次激活相同的操作,您可以延迟Click 处理程序中代码的执行,基于SystemInformation.DoubleClickTime 值(可由用户根据喜好设置)。

    然后,在Click 处理程序中执行一个操作,以防Double-Click 操作尚未执行。

    (您无法看到这两个处理程序的原因已经解释过了)

    bool doubleClicked = false;
    
    private async void listView1_Click(object sender, EventArgs e)
    {
        await Task.Delay(SystemInformation.DoubleClickTime);
    
        if (doubleClicked) {
            Console.WriteLine("ListView Click - Already Double-Clicked");
            doubleClicked = false;
        }
        else {
            Console.WriteLine("ListView Click");
        }
    }
    
    private void listView1_DoubleClick(object sender, EventArgs e)
    {
        doubleClicked = true;
        Console.WriteLine("ListView Double-Click");
    }
    

    【讨论】:

    • 这确实回答了我的问题。我可以将 MessageBox.Show 成功替换为控制台写行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多