C#使用timer实现的简单闹钟程序
作者:songguo 发布时间:2023-07-30 00:24:28
标签:C#,timer,闹钟
本文实例讲述了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;
}
}
}
完整实例代码点击此处本站下载。
希望本文所述对大家的C#程序设计有所帮助。


猜你喜欢
- synchronized关键字,一般称之为”同步锁“,用它来修饰需要同步的方法和需要同步代码块,默认是当前对象作为锁的对象。同步锁锁的是同一
- 在开发中经常使用到Set集合去重,那么去重的原理是怎样实现的呢?在此文章记录一下去重原理!!!下面是set集合类图下面我们来跟踪一下执行过程
- file.mkdir()创建单级文件夹,file.mkdirs()创建多级文件夹,file.createNewFile()创建的是一个文件。
- Mybatis 有两种实现方式其一:通过xml配置文件实现其二:面向接口编程的实现  
- 前言Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其
- SlidingMenu (侧滑菜单形式)在android开发过程中,经常用到,这次我们通过一个简单案例来仿写SlidingMenu 的大体功
- 在 Java 中,方法调用一般通过 Virtual Call 还有 Classic Call。Classic Call 就是直接指向方法的地
- 一、引言在刷算法的时候经常需要对数组进行排序,第一反应就是直接使用java.util包下的Arrays.sort()方法直接排序。但在刷算法
- 学习目的:1、掌握在Android中如何建立EditText2、掌握EditText的常用属性3、掌握EditText焦点的事件、按键的事件
- 1. 理解abstract:抽象的2. 作用abstract可以用来修饰类、方法。不能用abstract修饰变量、代码块、构造器。不能用ab
- 2PC两阶段提交协议分布式事务通常采用2PC协议,全称Two Phase Commitment Protocol。该协议主要为了解决在分布式
- 1.保存对象到文件中Java语言只能将实现了Serializable接口的类的对象保存到文件中,利用如下方法即可:public static
- 什么是Drawable首先Drawable是一个抽象类,表示的是可以在Canvas中绘制的图像,常被用作一个view的背景,有多种实现类完成
- java 中cookie的详解Java对cookie的操作比较简单,主要介绍下建立cookie和读取cookie,以及如何设定cookie的
- java 中设计模式(值对象)的实例详解应用场景:在Java开发时,需要来回交换大量的数据,比如要为方法传入参数,也要获取方法的返回值,该如
- 本文实例讲述了Asp.net中C#使用Socket发送和接收TCP数据的方法,分享给大家供大家参考。具体实现方法如下:具体程序代码如下:us
- 由于项目需求,需要为Java提供一套支持事件驱动机制的类库,可以实现类似于C#中的event和delegate机制。众所周知,Java语言本
- 一. 多维数组1. 概念多维数组可以看成是数组的数组。 比如二维数组就是一个特殊的一维数组,它的每个元素都是一个一维数组。其他多维数组的概念
- import java.io.File; public class ShowAllXML { public static void main
- 实现系统重启的APK需要system的权限,在AndroidManifest.xml中增加android:sharedUserId=&quo