C#使用timer實(shí)現(xiàn)的簡單鬧鐘程序
本文實(shí)例講述了C#使用timer實(shí)現(xiàn)的簡單鬧鐘程序。分享給大家供大家參考。具體如下:
當(dāng)我在電腦上工作,我經(jīng)常會(huì)被一些東西吸引,比如某寶,結(jié)果三個(gè)小時(shí)過去了我都完全沒有注意到。所以我通過C#做了一個(gè)簡單鬧鐘程序,這個(gè)小程序主要使用C# Timer對(duì)象,讓用戶設(shè)定一個(gè)倒計(jì)時(shí)的時(shí)長,如果時(shí)間到了,就播放一個(gè)wav音頻文件(也就是鬧鈴)。
我一直試圖保持這個(gè)timer的簡單性,但我還是添加了一些額外的功能,在狀態(tài)欄中顯示一個(gè)通知圖標(biāo)。
通過這個(gè)小應(yīng)用你也可以了解到C#中timer定時(shí)器的一些簡單用法。
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;
}
}
}
完整實(shí)例代碼點(diǎn)擊此處本站下載。
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#使用百度Ueditor富文本框?qū)崿F(xiàn)上傳文件
這篇文章主要為大家詳細(xì)介紹了C#如何使用百度Ueditor富文本框?qū)崿F(xiàn)上傳文件(圖片,視頻等),文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-07-07
C#實(shí)現(xiàn)在底圖上動(dòng)態(tài)生成文字和圖片
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)在底圖上動(dòng)態(tài)生成文字和圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
使用C# Winform應(yīng)用程序獲取網(wǎng)頁源文件的解決方法
本篇文章是對(duì)使用C# Winform應(yīng)用程序獲取網(wǎng)頁源文件的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
c# 如何對(duì)網(wǎng)絡(luò)信息進(jìn)行相關(guān)設(shè)置(ip,dns,網(wǎng)關(guān)等)
這篇文章主要介紹了c# 網(wǎng)絡(luò)適配器的相關(guān)操作,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03
C#編程自學(xué)之?dāng)?shù)據(jù)類型和變量三
C#語言類型系統(tǒng)提出的一個(gè)核心概念裝箱(boxing)拆箱(unboxing)。裝箱和取消裝箱的概念是C#的類型系統(tǒng)的核心。它在“值類型”和“引用類型”之間的架起了一座橋梁,使得任何“值類型”的值都可以轉(zhuǎn)換為object類型的值,反過來轉(zhuǎn)換也可以。2015-10-10
C#基礎(chǔ)語法:結(jié)構(gòu)和類區(qū)別詳解
這篇文章主要介紹了C#基礎(chǔ)語法:結(jié)構(gòu)和類詳解,本文總結(jié)了一些結(jié)構(gòu)和類的不同之處并給出了測試區(qū)別特性代碼,需要的朋友可以參考下2015-06-06-
最新評(píng)論

