属性:
Enabled:启用Elapsed事件的发生。
Interval:Elapsed事件的频率(ms为单位)。
事件:
Tick:每当经过指定的时间间隔时发生。
跑马灯练习:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 跑马灯事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = label1.Text.Substring(1) + label1.Text.Substring(0, 1);
//MessageBox.Show("Timer组件里的Tick事件//1s");
}
}
闹钟:(未完成:1、更改音乐的功能,2、关闭音乐)
public partial class Form1 : Form
{
public static int hour;
public static int minute;
public static int second;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 跑马灯事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = label1.Text.Substring(1) + label1.Text.Substring(0, 1);
//MessageBox.Show("Timer组件里的Tick事件//1s");
}
/// <summary>
/// 把当前时间赋值给labelTime
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer2_Tick(object sender, EventArgs e)
{
//实时时间
labelTime.Text = DateTime.Now.ToString();
//hour:minute:second播放音乐
if (DateTime.Now.Hour == hour && DateTime.Now.Minute == minute && DateTime.Now.Second == second)
{
//播放音乐
SoundPlayer sp = new SoundPlayer();
sp.SoundLocation = @"C:\Users\Public\Documents\Adobe\Premiere Pro\14.0\Tutorial\Going Home project\Audio\APM_Adobe_Going Home_v3.wav";
sp.Play();
}
}
private void labelTime_Click(object sender, EventArgs e)
{
}
/// <summary>
/// 当窗体加载的时候触发,使label获得现在的本地时间
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
labelTime.Text = DateTime.Now.ToString();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
/// <summary>
/// 将用户输入的时分秒赋值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSet_Click(object sender, EventArgs e)
{
hour = Convert.ToInt32(txtHour.Text);
minute = Convert.ToInt32(txtMin.Text);
second = Convert.ToInt32(txtSec.Text);
MessageBox.Show("设置成功");
}
/// <summary>
/// 关闭程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnQuit_Click(object sender, EventArgs e)
{
this.Close();
//MessageBox.Show("关闭成功");
}
/// <summary>
/// 重置用户输入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnReset_Click(object sender, EventArgs e)
{
//用户点击后清空三个文本框
txtHour.Clear();
txtMin.Clear();
txtSec.Clear();
//让焦点到TxtHour中
txtHour.Focus();
}
}