0

我正在尝试使用 C# 中的表单制作时钟,但是因为 UI 线程使我无法在 UI 线程中使用 while 循环和类似的东西,所以我使用计时器每半秒更新一次以更新标签当时间改变时依次更新。当前设置为“00 00 00”,并在程序加载到当前数字时间时更新一次。(我每半秒更新一次,以避免时钟与实时时钟不同步)我的问题是,即使我使用的是计时器,它也只更新一次,而不是每半秒更新一次标签。我很确定我的代码是正确的,但可能不在正确的地方。

这是我的代码:

MainForm.Designer.cs:

using System;
namespace alarmClock
{
partial class MainForm
{
    System.ComponentModel.IContainer components = null;

    protected override void Dispose(bool disposing)
    {
        if (disposing) {
            if (components != null) {
                components.Dispose();
            }
        }
        base.Dispose(disposing);
    }

    public void setHourTime() {
        setHour.Text = MainForm.setH.ToString();
        if(MainForm.setH < 10) {
            setHour.Text = "0" + MainForm.setH.ToString();
        }
        if(MainForm.setH > 24) {
            MainForm.setH=0;
            setHour.Text = "0" + MainForm.setH.ToString();
        }
    }
    public void setMinuteTime() {
        setMinute.Text = MainForm.setM.ToString();
        if(MainForm.setM < 10) {
            setMinute.Text = "0" + MainForm.setM.ToString();
        }
        if(MainForm.setM > 60) {
            MainForm.setM=0;
            setMinute.Text = "0" + MainForm.setM.ToString();
        }
    }
    public void setSecondTime() {
        setSecond.Text = MainForm.setS.ToString();
        if(MainForm.setS < 10) {
            setSecond.Text = "0" + MainForm.setS.ToString();
        }
        if(MainForm.setS > 60) {
            MainForm.setS=0;
            setSecond.Text = "0" + MainForm.setS.ToString();
        }
    }

    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
        this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
        this.minute = new System.Windows.Forms.Label();
        this.hour = new System.Windows.Forms.Label();
        this.second = new System.Windows.Forms.Label();
        this.checkboxTime = new System.Windows.Forms.CheckedListBox();
        this.delTime = new System.Windows.Forms.Button();
        this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel();
        this.tableLayoutPanel3 = new System.Windows.Forms.TableLayoutPanel();
        this.setHour = new System.Windows.Forms.Label();
        this.setMinute = new System.Windows.Forms.Label();
        this.setSecond = new System.Windows.Forms.Label();
        this.tableLayoutPanel4 = new System.Windows.Forms.TableLayoutPanel();
        this.subHour = new System.Windows.Forms.Button();
        this.addHour = new System.Windows.Forms.Button();
        this.subMin = new System.Windows.Forms.Button();
        this.addMin = new System.Windows.Forms.Button();
        this.subSec = new System.Windows.Forms.Button();
        this.addSec = new System.Windows.Forms.Button();
        this.setTime = new System.Windows.Forms.GroupBox();
        this.addTime = new System.Windows.Forms.Button();
        this.clockTimer = new System.Windows.Forms.Timer(this.components);
        this.tableLayoutPanel1.SuspendLayout();
        this.tableLayoutPanel2.SuspendLayout();
        this.tableLayoutPanel3.SuspendLayout();
        this.tableLayoutPanel4.SuspendLayout();
        this.setTime.SuspendLayout();
        this.SuspendLayout();
        // 
        // tableLayoutPanel1
        // 
        resources.ApplyResources(this.tableLayoutPanel1, "tableLayoutPanel1");
        this.tableLayoutPanel1.Controls.Add(this.minute, 1, 0);
        this.tableLayoutPanel1.Controls.Add(this.hour, 0, 0);
        this.tableLayoutPanel1.Controls.Add(this.second, 2, 0);
        this.tableLayoutPanel1.Name = "tableLayoutPanel1";
        // 
        // minute
        // 
        resources.ApplyResources(this.minute, "minute");
        this.minute.ForeColor = System.Drawing.Color.Black;
        this.minute.Name = "minute";
        // 
        // hour
        // 
        resources.ApplyResources(this.hour, "hour");
        this.hour.ForeColor = System.Drawing.Color.Black;
        this.hour.Name = "hour";
        // 
        // second
        // 
        resources.ApplyResources(this.second, "second");
        this.second.ForeColor = System.Drawing.Color.Black;
        this.second.Name = "second";
        // 
        // checkboxTime
        // 
        this.checkboxTime.BackColor = System.Drawing.Color.Silver;
        resources.ApplyResources(this.checkboxTime, "checkboxTime");
        this.checkboxTime.ForeColor = System.Drawing.Color.Black;
        this.checkboxTime.FormattingEnabled = true;
        this.checkboxTime.Items.AddRange(new object[] {
                                resources.GetString("checkboxTime.Items"),
                                resources.GetString("checkboxTime.Items1")});
        this.checkboxTime.Name = "checkboxTime";
        this.checkboxTime.SelectedIndexChanged += new System.EventHandler(this.CheckboxTimeSelectedIndexChanged);
        // 
        // delTime
        // 
        resources.ApplyResources(this.delTime, "delTime");
        this.delTime.Name = "delTime";
        this.delTime.UseVisualStyleBackColor = true;
        this.delTime.Click += new System.EventHandler(this.DelTimeClick);
        // 
        // tableLayoutPanel2
        // 
        resources.ApplyResources(this.tableLayoutPanel2, "tableLayoutPanel2");
        this.tableLayoutPanel2.Controls.Add(this.tableLayoutPanel3, 0, 1);
        this.tableLayoutPanel2.Controls.Add(this.tableLayoutPanel4, 0, 0);
        this.tableLayoutPanel2.Name = "tableLayoutPanel2";
        // 
        // tableLayoutPanel3
        // 
        resources.ApplyResources(this.tableLayoutPanel3, "tableLayoutPanel3");
        this.tableLayoutPanel3.Controls.Add(this.setHour, 0, 0);
        this.tableLayoutPanel3.Controls.Add(this.setMinute, 1, 0);
        this.tableLayoutPanel3.Controls.Add(this.setSecond, 2, 0);
        this.tableLayoutPanel3.Name = "tableLayoutPanel3";
        // 
        // setHour
        // 
        resources.ApplyResources(this.setHour, "setHour");
        this.setHour.ForeColor = System.Drawing.Color.Black;
        this.setHour.Name = "setHour";
        // 
        // setMinute
        // 
        resources.ApplyResources(this.setMinute, "setMinute");
        this.setMinute.ForeColor = System.Drawing.Color.Black;
        this.setMinute.Name = "setMinute";
        // 
        // setSecond
        // 
        resources.ApplyResources(this.setSecond, "setSecond");
        this.setSecond.ForeColor = System.Drawing.Color.Black;
        this.setSecond.Name = "setSecond";
        // 
        // tableLayoutPanel4
        // 
        resources.ApplyResources(this.tableLayoutPanel4, "tableLayoutPanel4");
        this.tableLayoutPanel4.Controls.Add(this.subHour, 0, 0);
        this.tableLayoutPanel4.Controls.Add(this.addHour, 1, 0);
        this.tableLayoutPanel4.Controls.Add(this.subMin, 2, 0);
        this.tableLayoutPanel4.Controls.Add(this.addMin, 3, 0);
        this.tableLayoutPanel4.Controls.Add(this.subSec, 4, 0);
        this.tableLayoutPanel4.Controls.Add(this.addSec, 5, 0);
        this.tableLayoutPanel4.Name = "tableLayoutPanel4";
        // 
        // subHour
        // 
        resources.ApplyResources(this.subHour, "subHour");
        this.subHour.Name = "subHour";
        this.subHour.UseVisualStyleBackColor = true;
        this.subHour.Click += new System.EventHandler(this.SubHourClick);
        // 
        // addHour
        // 
        resources.ApplyResources(this.addHour, "addHour");
        this.addHour.Name = "addHour";
        this.addHour.UseVisualStyleBackColor = true;
        this.addHour.Click += new System.EventHandler(this.AddHourClick);
        // 
        // subMin
        // 
        resources.ApplyResources(this.subMin, "subMin");
        this.subMin.Name = "subMin";
        this.subMin.UseVisualStyleBackColor = true;
        this.subMin.Click += new System.EventHandler(this.SubMinClick);
        // 
        // addMin
        // 
        resources.ApplyResources(this.addMin, "addMin");
        this.addMin.Name = "addMin";
        this.addMin.UseVisualStyleBackColor = true;
        this.addMin.Click += new System.EventHandler(this.AddMinClick);
        // 
        // subSec
        // 
        resources.ApplyResources(this.subSec, "subSec");
        this.subSec.Name = "subSec";
        this.subSec.UseVisualStyleBackColor = true;
        this.subSec.Click += new System.EventHandler(this.SubSecClick);
        // 
        // addSec
        // 
        resources.ApplyResources(this.addSec, "addSec");
        this.addSec.Name = "addSec";
        this.addSec.UseVisualStyleBackColor = true;
        this.addSec.Click += new System.EventHandler(this.AddSecClick);
        // 
        // setTime
        // 
        resources.ApplyResources(this.setTime, "setTime");
        this.setTime.BackColor = System.Drawing.Color.Silver;
        this.setTime.Controls.Add(this.addTime);
        this.setTime.Controls.Add(this.tableLayoutPanel2);
        this.setTime.Name = "setTime";
        this.setTime.TabStop = false;
        // 
        // addTime
        // 
        resources.ApplyResources(this.addTime, "addTime");
        this.addTime.Name = "addTime";
        this.addTime.UseVisualStyleBackColor = true;
        this.addTime.Click += new System.EventHandler(this.AddTimeClick);
        // 
        // clockTimer
        // 
        this.clockTimer.Enabled = true;
        this.clockTimer.Interval = 500;
        this.clockTimer.Tick += new System.EventHandler(this.ClockTimerTick);
        // 
        // MainForm
        // 
        resources.ApplyResources(this, "$this");
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.Color.DarkGray;
        this.Controls.Add(this.setTime);
        this.Controls.Add(this.delTime);
        this.Controls.Add(this.checkboxTime);
        this.Controls.Add(this.tableLayoutPanel1);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
        this.Name = "MainForm";
        this.tableLayoutPanel1.ResumeLayout(false);
        this.tableLayoutPanel1.PerformLayout();
        this.tableLayoutPanel2.ResumeLayout(false);
        this.tableLayoutPanel2.PerformLayout();
        this.tableLayoutPanel3.ResumeLayout(false);
        this.tableLayoutPanel3.PerformLayout();
        this.tableLayoutPanel4.ResumeLayout(false);
        this.setTime.ResumeLayout(false);
        this.setTime.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();
    }
    public System.Windows.Forms.Timer clockTimer;
    System.Windows.Forms.Button addTime;
    System.Windows.Forms.GroupBox setTime;
    System.Windows.Forms.Button addSec;
    System.Windows.Forms.Button subSec;
    System.Windows.Forms.Button addMin;
    System.Windows.Forms.Button subMin;
    System.Windows.Forms.Button addHour;
    System.Windows.Forms.TableLayoutPanel tableLayoutPanel4;
    System.Windows.Forms.Label setSecond;
    System.Windows.Forms.Label setMinute;
    System.Windows.Forms.Label setHour;
    System.Windows.Forms.TableLayoutPanel tableLayoutPanel3;
    System.Windows.Forms.Button subHour;
    System.Windows.Forms.TableLayoutPanel tableLayoutPanel2;
    System.Windows.Forms.Button delTime;
    System.Windows.Forms.CheckedListBox checkboxTime;
    System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
    System.Windows.Forms.Label second;
    System.Windows.Forms.Label hour;
    System.Windows.Forms.Label minute;
    }
}

MainForm.cs:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace alarmClock
{
public partial class MainForm : Form
{
    public static int setH = 0;
    public static int setM = 0;
    public static int setS = 0;

    public static DateTime now = DateTime.Now;
    public static string time = now.ToLongTimeString();

    public MainForm()
    {
        InitializeComponent();
    }
    void DelTimeClick(object sender, EventArgs e)
    {

    }

    void SubHourClick(object sender, EventArgs e)
    {
        setH--;
        setHourTime();
    }

    void AddHourClick(object sender, EventArgs e)
    {
        setH++;
        setHourTime();
    }

    void SubMinClick(object sender, EventArgs e)
    {
        setM--;
        setMinuteTime();
    }

    void AddMinClick(object sender, EventArgs e)
    {
        setM++;
        setMinuteTime();
    }

    void SubSecClick(object sender, EventArgs e)
    {
        setS--;
        setSecondTime();
    }

    void AddSecClick(object sender, EventArgs e)
    {
        setS++;
        setSecondTime();
    }

    void AddTimeClick(object sender, EventArgs e)
    {

    }


    void CheckboxTimeSelectedIndexChanged(object sender, EventArgs e)
    {

    }

    void ClockTimerTick(object sender, EventArgs e)
    {
        hour.Text = time[0].ToString() + time[1].ToString();
        hour.Update();
        minute.Text = time[3].ToString() + time[4].ToString();
        minute.Update();
        second.Text = time[6].ToString() + time[7].ToString();
        second.Update();
        }
    }
}
4

1 回答 1

0

ClockTimerTick 处理程序将time变量的内容复制到小时、分钟和秒控件。而且这个变量永远不会改变,你只设置一次。

于 2013-10-14T15:07:50.280 回答