属性:
    Enabled:启用Elapsed事件的发生。
    Interval:Elapsed事件的频率(ms为单位)。

    事件:
    Tick:每当经过指定的时间间隔时发生。

    跑马灯练习:
    image.png

    1. public partial class Form1 : Form
    2. {
    3. public Form1()
    4. {
    5. InitializeComponent();
    6. }
    7. /// <summary>
    8. /// 跑马灯事件
    9. /// </summary>
    10. /// <param name="sender"></param>
    11. /// <param name="e"></param>
    12. private void timer1_Tick(object sender, EventArgs e)
    13. {
    14. label1.Text = label1.Text.Substring(1) + label1.Text.Substring(0, 1);
    15. //MessageBox.Show("Timer组件里的Tick事件//1s");
    16. }
    17. }

    闹钟:(未完成:1、更改音乐的功能,2、关闭音乐)
    image.png

    image.png

    1. public partial class Form1 : Form
    2. {
    3. public static int hour;
    4. public static int minute;
    5. public static int second;
    6. public Form1()
    7. {
    8. InitializeComponent();
    9. }
    10. /// <summary>
    11. /// 跑马灯事件
    12. /// </summary>
    13. /// <param name="sender"></param>
    14. /// <param name="e"></param>
    15. private void timer1_Tick(object sender, EventArgs e)
    16. {
    17. label1.Text = label1.Text.Substring(1) + label1.Text.Substring(0, 1);
    18. //MessageBox.Show("Timer组件里的Tick事件//1s");
    19. }
    20. /// <summary>
    21. /// 把当前时间赋值给labelTime
    22. /// </summary>
    23. /// <param name="sender"></param>
    24. /// <param name="e"></param>
    25. private void timer2_Tick(object sender, EventArgs e)
    26. {
    27. //实时时间
    28. labelTime.Text = DateTime.Now.ToString();
    29. //hour:minute:second播放音乐
    30. if (DateTime.Now.Hour == hour && DateTime.Now.Minute == minute && DateTime.Now.Second == second)
    31. {
    32. //播放音乐
    33. SoundPlayer sp = new SoundPlayer();
    34. sp.SoundLocation = @"C:\Users\Public\Documents\Adobe\Premiere Pro\14.0\Tutorial\Going Home project\Audio\APM_Adobe_Going Home_v3.wav";
    35. sp.Play();
    36. }
    37. }
    38. private void labelTime_Click(object sender, EventArgs e)
    39. {
    40. }
    41. /// <summary>
    42. /// 当窗体加载的时候触发,使label获得现在的本地时间
    43. /// </summary>
    44. /// <param name="sender"></param>
    45. /// <param name="e"></param>
    46. private void Form1_Load(object sender, EventArgs e)
    47. {
    48. labelTime.Text = DateTime.Now.ToString();
    49. }
    50. private void label1_Click(object sender, EventArgs e)
    51. {
    52. }
    53. private void label3_Click(object sender, EventArgs e)
    54. {
    55. }
    56. /// <summary>
    57. /// 将用户输入的时分秒赋值
    58. /// </summary>
    59. /// <param name="sender"></param>
    60. /// <param name="e"></param>
    61. private void btnSet_Click(object sender, EventArgs e)
    62. {
    63. hour = Convert.ToInt32(txtHour.Text);
    64. minute = Convert.ToInt32(txtMin.Text);
    65. second = Convert.ToInt32(txtSec.Text);
    66. MessageBox.Show("设置成功");
    67. }
    68. /// <summary>
    69. /// 关闭程序
    70. /// </summary>
    71. /// <param name="sender"></param>
    72. /// <param name="e"></param>
    73. private void btnQuit_Click(object sender, EventArgs e)
    74. {
    75. this.Close();
    76. //MessageBox.Show("关闭成功");
    77. }
    78. /// <summary>
    79. /// 重置用户输入
    80. /// </summary>
    81. /// <param name="sender"></param>
    82. /// <param name="e"></param>
    83. private void btnReset_Click(object sender, EventArgs e)
    84. {
    85. //用户点击后清空三个文本框
    86. txtHour.Clear();
    87. txtMin.Clear();
    88. txtSec.Clear();
    89. //让焦点到TxtHour中
    90. txtHour.Focus();
    91. }
    92. }