我的申请如下
program.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;
}
}
我想同时处理click和doubleclick事件 但是我只能得到显示的Click事件消息。
答案 0 :(得分:4)
这是因为您在单次点击
也就是说,您在单击(带有{{1} ),系统将无法识别双击。超过了它认为下次点击(在对话框显示时按住)的时间。分开双击序列。
相反,请使用MessageBox
并在output window中查看结果
Debug.WriteLine
Jakob Busk Sørensen的精彩评论
这也意味着您可以单击使用的功能受到限制 如果您还想使用双击。基本上任何东西 单击运行,也将双击运行。或者 考虑根据您要执行的操作单击右键。
答案 1 :(得分:0)
如果您要同时处理Click
和Double-Click
并执行不同的操作,或者要防止一次激活两次相同的操作,则可以延迟{{1}中的代码的执行}处理程序,基于SystemInformation.DoubleClickTime值(可由用户根据偏好设置)。
然后,在尚未执行Click
动作的情况下,在Click
处理程序中执行一个动作。
(已经解释了为什么不能同时看到两个处理程序的原因)
Double-Click