繁体   English   中英

C#中如何检测并记录按钮按下的时间?

[英]How to detect and record the time when the button is pressed in C#?

例如,如果有一个按钮,当用户按下按钮时,系统应该检测并记录 dd-mm-yy 中的时间。 但是,如果用户在 2 秒内多次按下按钮,系统应该只记录用户第一次按下按钮的时间。

假设:您希望收集点击次数,但不超过每 2 秒一次。 这个简单的表格就可以了。

代码:

namespace RecordClicks {
        public partial class Form1 : Form {
    
            private DateTime _recordedClick = DateTime.MinValue;
    
            public Form1() {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e) {
    
                DateTime currentClickTime = DateTime.Now;
    
                if (currentClickTime.Subtract(_recordedClick).TotalMilliseconds > 2000 ) {
                    _recordedClick = currentClickTime;
                    lblClick.Text = currentClickTime.ToString();
                    // TODO: add code to record this click
    
                }
    
            }
        }
    }

设计师:

namespace RecordClicks {
    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.button1 = new System.Windows.Forms.Button();
            this.lblClick = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(121, 241);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(132, 53);
            this.button1.TabIndex = 0;
            this.button1.Text = "Clicker";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // lblClick
            // 
            this.lblClick.AutoSize = true;
            this.lblClick.Font = new System.Drawing.Font("Segoe UI", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            this.lblClick.Location = new System.Drawing.Point(121, 127);
            this.lblClick.Name = "lblClick";
            this.lblClick.Size = new System.Drawing.Size(90, 45);
            this.lblClick.TabIndex = 1;
            this.lblClick.Text = "Time";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.lblClick);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Clicker Test";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private Button button1;
        private Label lblClick;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM