今天,开始学习C#,主要是依赖C#语言开发应用程序,特别是串口助手,以此来实现对单片机的串口数据读取。
话不多说,今天根据学习视频写了下定时器的一个小小程序,菜鸟入坑。
以下是具体的实现代码:
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 定时器
{
public partial class Form1 : Form
{
int count=0;
int timer;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int i;
for (i = 1; i <= 100;i++) //计数范围:0~99
{
comboBox1.Items.Add(i.ToString()+" 秒");
}
label3.Text = "0秒";
comboBox1.Text = "10 秒";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
count++; //记录当前的时间值
label3.Text = (timer - count).ToString()+"秒";
progressBar1.Value = count;
if (count == timer)
{
timer1.Stop(); //时间到,停止计时
System.Media.SystemSounds.Asterisk.Play(); //提示音
MessageBox.Show("时间到!","提示"); //弹出提示框
}
}
private void button1_Click(object sender, EventArgs e)
{
string str;
str = comboBox1.Text; //得到字符串
timer = Convert.ToInt16(str.Substring(0, 2)); //得到设定的数据值(整型)
progressBar1.Maximum = timer; //进度条最大数值
timer1.Start(); //定时器开始计时
}
}
}
程序界面很简单,
就是实现对设定时间的简单计时。