【问题标题】:How to detect and record the time when the button is pressed in C#?C#中如何检测并记录按下按钮的时间?
【发布时间】:2022-01-28 13:13:38
【问题描述】:

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

【问题讨论】:

  • 请向我们展示您的尝试
  • 嗨 Jazb,我没有尝试编码,因为我对这个问题一无所知。
  • 发帖者"is trying to do something specific"时,有时可以不用代码提问。但是,我们可能需要先了解一些事情。这是什么类型的应用程序?表格? WPF?网络?
  • 应用的类型是WPF

标签: c# button time recorder


【解决方案1】:

假设:您希望收集点击次数,但不超过每 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;
    }
}

【讨论】:

  • 感谢您提供的代码。但是,应用程序的类型不是形式。那有什么方法可以记录在WPF中按下按钮的时间吗?
  • 同一个概念,代码只是演示
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多