单击在文本框中显示当前时间的事件时出错

本文关键字:时间 事件 出错 文本 显示 单击 | 更新日期: 2023-09-27 18:17:07

好吧,所以我想采取当前的时间,然后把它放在TextBox我在Windows窗体后点击一个按钮。到目前为止我得到了这么多。但是我遇到了很多问题,我已经放弃了,所以我来这里寻求帮助。顺便说一句,这可能吗?

:

1)没有重载'OnTimedEvent'匹配委托"System.Timers.ElapsedEventHandler"

2)成员时间。button1_Click(object, System.EventArgs)'不能通过实例引用访问;用类型名限定它而不是

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Time {
    public partial class Form1: Form {
        public Form1() {
            InitializeComponent();
        }
        public static System.Timers.Timer aTimer;
        private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e, TextBox textBox1) {
            textBox1.Text = DateTime.Now.ToString("h:mm:ss tt");
        }
        public static void button1_Click(object sender, EventArgs e) {
            aTimer = new System.Timers.Timer(1000);
            aTimer.Elapsed += OnTimedEvent;
            aTimer.AutoReset = true;
            aTimer.Enabled = true;
        }
    }
}

单击在文本框中显示当前时间的事件时出错

您已经更改了OnTimedEvent()方法的签名,这将不起作用。

删除最后一个参数,并删除static关键字从所有3个地方你目前有它。

那么下面的事件方法应该可以工作:

private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
    textBox1.Text = DateTime.Now.ToString("h:mm:ss tt");
}