• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#使用timer实现的简单闹钟程序

C#使用timer实现的简单闹钟程序

作者:songguo 字体:[增加 减小] 来源:互联网 时间:2017-05-28

songguo 通过本文主要向大家介绍了c#中timer的用法,c#中timer控件,c#timer控件的用法,c#timer,c#timer计时器等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了C#使用timer实现的简单闹钟程序。分享给大家供大家参考。具体如下:

当我在电脑上工作,我经常会被一些东西吸引,比如某宝,结果三个小时过去了我都完全没有注意到。所以我通过C#做了一个简单闹钟程序,这个小程序主要使用C# Timer对象,让用户设定一个倒计时的时长,如果时间到了,就播放一个wav音频文件(也就是闹铃)。

我一直试图保持这个timer的简单性,但我还是添加了一些额外的功能,在状态栏中显示一个通知图标。
通过这个小应用你也可以了解到C#中timer定时器的一些简单用法。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Timers;
using System.IO;
using System.Reflection;
namespace timerAlarm
{
  public class TimerForm : System.Windows.Forms.Form
  {
    //Controls and Components
    private System.Windows.Forms.TextBox timerInput;
    private System.Windows.Forms.Button StartButton;
    private System.Windows.Forms.Button ResetButton;
    private System.ComponentModel.IContainer components;
    //Timer and associated variables
    private System.Timers.Timer timerClock = new System.Timers.Timer();
    private int clockTime = 0;
    private int alarmTime = 0;
    public TimerForm()
    {
      InitializeComponent();
      InitializeTimer();
    }
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null)
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }
    #region Windows Form Designer generated code
    /// <SUMMARY>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </SUMMARY>
    private void InitializeComponent()
    {
      this.components = new System.ComponentModel.Container();
      System.Resources.ResourceManager resources = new System.
        Resources.ResourceManager(typeof(TimerForm));
      this.timerInput = new System.Windows.Forms.TextBox();
      this.StartButton = new System.Windows.Forms.Button();
      this.ResetButton = new System.Windows.Forms.Button();
      this.SuspendLayout();
      //
      // timerInput
      //
      this.timerInput.Location = new System.Drawing.Point(12, 13);
      this.timerInput.Name = "timerInput";
      this.timerInput.Size = new System.Drawing.Size(50, 20);
      this.timerInput.TabIndex = 0;
      this.timerInput.Text = "00:00:00";
      //
      // StartButton
      //
      this.StartButton.FlatStyle = System.Windows.Forms.
        FlatStyle.System;
      this.StartButton.Location = new System.Drawing.Point(75, 11);
      this.StartButton.Name = "StartButton";
      this.StartButton.TabIndex = 1;
      this.StartButton.Text = "Start";
      this.StartButton.Click += new System.EventHandler
        (this.StartButton_Click);
      //
      // ResetButton
      //
      this.ResetButton.FlatStyle = System.Windows.Forms.
        FlatStyle.System;
      this.ResetButton.Location = new System.Drawing.Point(161, 11);
      this.ResetButton.Name = "ResetButton";
      this.ResetButton.TabIndex = 2;
      this.ResetButton.Text = "Reset";
      this.ResetButton.Click += new
        System.EventHandler(this.ResetButton_Click);
      //
      // TimerForm
      //
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(247, 46);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
         this.ResetButton,
         this.StartButton,
         this.timerInput});
      this.FormBorderStyle = System.Windows.Forms.
        FormBorderStyle.FixedSingle;
      this.Icon = ((System.Drawing.Icon)(resources.
        GetObject("$this.Icon")));
      this.MaximizeBox = false;
      this.Name = "TimerForm";
      this.StartPosition = System.Windows.Forms.
        FormStartPosition.CenterScreen;
      this.Text = "Alarm Timer";
      this.Resize += new System.EventHandler(this.
        TimerForm_Resized);
      this.ResumeLayout(false);
    }
    #endregion
    public void InitializeTimer()
    {
      this.timerClock.Elapsed += new ElapsedEventHandler(OnTimer);
      this.timerClock.Interval = 1000;
      this.timerClock.Enabled = true;
    }
    [STAThread]
    static void Main()
    {
      Application.Run(new TimerForm());
    }
    private void TimerForm_Resized(object sender, System.EventArgs e)
    {
      if( this.WindowState == FormWindowState.Minimized )
      {
        this.Hide();
      }
    }
    private void StartButton_Click(object sender, System.EventArgs e)
    {
      this.clockTime = 0;
      inputToSeconds( this.timerInput.Text );
    }
    private void ResetButton_Click(object sender, System.EventArgs e)
    {
      try
      {
        this.clockTime = 0;
        this.alarmTime = 0;
        this.timerInput.Text = "00:00:00";
      }
      catch( Exception ex )
      {
        MessageBox.Show("ResetButton_Click(): " + ex.Message );
      }
    }
    public void OnTimer(Object source, ElapsedEventArgs e)
    {
      try
      {
        this.clockTime++;
        int countdown = this.alarmTime - this.clockTime ;
        if( this.alarmTime != 0 )
        {
          this.timerInput.Text = secondsToTime(countdown);
        }
        //Sound Alarm
        if( this.clockTime == this.alarmTime )
        {
          MessageBox.Show("Play Sound");
        }
      }
      catch( Exception ex )
      {
        MessageBox.Show("OnTimer(): " + ex.Message );
      }    
    }
    private void inputToSeconds( string timerInput )
    {
      try
      {
        string[] timeArray = new string[3];
        int minutes = 0;
        int hours = 0;
        int seconds = 0;
        int occurence = 0;
        int length = 0;
        occurence = timerInput.LastIndexOf(":");
        length = timerInput.Length;
        //Check for invalid input
        if( occurence == -1 || length != 8 )
        {
          MessageBox.Show("Invalid Time Format.");
          ResetButton_Click( null, null );
        }
        else
        {
          timeArray = timerInput.Split(':');
          seconds = Convert.ToInt32( timeArray[2] );
          minutes = Convert.ToInt32( timeArray[1] );
          hours = Convert.ToInt32( timeArray[0] );
          this.alarmTime += seconds;
          this.alarmTime += minutes*60;
          this.alarmTime += (hours*60)*60;
        }
      }
      catch( Exception e )
      {
        MessageBox.Show("inputToSeconds(): " + e.Message );
      }
    }
    public string secondsToTime( int seconds )
    {
      int minutes = 0;
      int hours = 0;
      while( seconds >= 60 )
      {
        minutes += 1;
        seconds -= 60;
      }
      while( minutes >= 60 )
      {
        hours += 1;
        minutes -= 60;
      }
      string strHours = hours.ToString();
      string strMinutes = minutes.ToString();
      string strSeconds = seconds.ToString();
      if( strHours.Length < 2 )
        strHours = "0" + strHours;
      if( strMinutes.Length < 2 )
        strMinutes = "0" + strMinutes;
      if( strSeconds.Length < 2 )
        strSeconds = "0" + strSeconds;
      return strHours + ":" + strMinutes + ":" + strSeconds;
    }
  }
}
</div>

完整实例代码点击此处本站下载。

希望本文所述对大家的C#程序设计有所帮助。

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C#中timer定时器用法实例
  • C#使用timer实现的简单闹钟程序
  • C#使用timer定时在屏幕上输出信息的方法
  • c#各种Timer类的区别与用法介绍
  • C#中timer类的用法总结

相关文章

  • 2017-05-28C#控制台程序使用Log4net日志组件详解
  • 2017-05-28浅谈C#手机号换成111XXXX1111 这种显示的解决思路
  • 2017-05-28Chrome Visual Studio 2005下的编译过程
  • 2017-05-28C#数组中List, Dictionary的相互转换问题
  • 2017-05-28C#中的事务用法实例分析
  • 2017-05-28C#.NET 图片水印添加代码
  • 2017-05-28C#中WinForm控件的拖动和缩放的实现代码
  • 2017-05-28C# 设计模式系列教程-命令模式
  • 2017-05-28C# 调用Delphi dll 实例代码
  • 2017-05-28C# 泛型数组学习小结

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C# 7.0 新特性1之基于Tuple的“多”返回值方法
    • C#实现将像素转换为页面单位的方法
    • C#执行DOS命令的方法
    • C# DatagridView常用操作汇总
    • .NET企业级项目中遇到的国际化问题和解决方法
    • c#保存窗口位置大小操作类(序列化和文件读写功能)
    • C# DropDownList中点击打开新窗口的方法
    • C#中判断本地系统的网络连接状态的方法
    • C#读取二进制文件方法分析
    • WPF字体或内容模糊的解决方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有