用Timer控件实现简单的闹钟。
工具/原料
-
Microsoft Visual Studio 2015
方法/步骤
-
1
新建一个C#应用程序,应用程序命名为ClockDlg。
-
2
窗体界面设计如下:
-
3
双击窗体添加响应函数Form1_Load(),响应函数Form1_Load()中添加一下代码:
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 1000;//1秒
timer1.Enabled = true;
label2.Text = DateTime.Now.ToString();
}
-
4
双击Timer控件为其添加响应函数timer1_Tick(),并添加一下代码。
private void timer1_Tick(object sender, EventArgs e)
{
DateTime time = DateTime.Now;
label2.Font = new Font("黑体", 12);
label2.Text = time.ToString();
if (time.Hour == 17 && time.Minute == 8 && time.Second == 1)
{
MessageBox.Show("闹钟时间到了!");
}
}
-
5
程序打开运行如下:
-
6
当时间到了设置好的时间闹钟就会给予提示。
END
注意事项
-
注意Timer控件的使用。
文章评论