最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#使用timer實現(xiàn)的簡單鬧鐘程序

 更新時間:2015年03月31日 09:56:27   作者:songguo  
這篇文章主要介紹了C#使用timer實現(xiàn)的簡單鬧鐘程序,涉及timer控件的使用及音頻文件的操作技巧,非常具有實用價值,需要的朋友可以參考下

本文實例講述了C#使用timer實現(xiàn)的簡單鬧鐘程序。分享給大家供大家參考。具體如下:

當我在電腦上工作,我經(jīng)常會被一些東西吸引,比如某寶,結(jié)果三個小時過去了我都完全沒有注意到。所以我通過C#做了一個簡單鬧鐘程序,這個小程序主要使用C# Timer對象,讓用戶設(shè)定一個倒計時的時長,如果時間到了,就播放一個wav音頻文件(也就是鬧鈴)。

我一直試圖保持這個timer的簡單性,但我還是添加了一些額外的功能,在狀態(tài)欄中顯示一個通知圖標。
通過這個小應(yīng)用你也可以了解到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#程序設(shè)計有所幫助。

相關(guān)文章

  • C#事件訪問器詳解

    C#事件訪問器詳解

    C#事件訪問器詳細解析。下面示例演示了具有訪問器的聲明.兩個訪問器都有叫做value的隱式值參數(shù),它接受實例或靜態(tài)方法的引用
    2014-01-01
  • C#實現(xiàn)桶排序算法的示例代碼

    C#實現(xiàn)桶排序算法的示例代碼

    桶排序是一種快速且高效的排序算法,通過將數(shù)據(jù)分配到有序桶中并分別排序,適合均勻分布數(shù)據(jù),它的時間復(fù)雜度為O(n),但不適合數(shù)據(jù)分布極不均勻或數(shù)據(jù)范圍很大的情況,桶排序算法簡單、易實現(xiàn),可調(diào)整桶的大小和數(shù)量以適應(yīng)不同數(shù)據(jù),感興趣的可以了解一下
    2024-10-10
  • C#讀寫Excel的流程步驟

    C#讀寫Excel的流程步驟

    這篇文章主要介紹了詳解C#讀寫Excel的流程步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或工作有一定的參考學習價值,需要的朋友們下面隨著小編來一起來學習吧
    2023-12-12
  • C#中弱引用使用小結(jié)

    C#中弱引用使用小結(jié)

    弱引用是一種不會阻止垃圾回收的對象引用,適用于緩存和事件監(jiān)聽等場景,但需注意其不確定性和潛在的性能影響,下面就來詳細的介紹C#中弱引用使用小結(jié),感興趣的可以了解一下
    2025-12-12
  • NumberToUpper數(shù)字轉(zhuǎn)中文詳解

    NumberToUpper數(shù)字轉(zhuǎn)中文詳解

    本文介紹NumberToUpper數(shù)字轉(zhuǎn)中文的方法,大家參考使用吧
    2013-12-12
  • 詳解CLR的內(nèi)存分配和回收機制

    詳解CLR的內(nèi)存分配和回收機制

    本文詳細講解了CLR的內(nèi)存分配和回收機制,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • C#之Socket操作類實例解析

    C#之Socket操作類實例解析

    這篇文章主要介紹了C#的Socket操作類用法,需要的朋友可以參考下
    2014-08-08
  • Silverlight實現(xiàn)跑馬燈動畫

    Silverlight實現(xiàn)跑馬燈動畫

    這篇文章主要為大家詳細介紹了Silverlight實現(xiàn)跑馬燈動畫,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • C#使用foreach語句簡單遍歷數(shù)組的方法

    C#使用foreach語句簡單遍歷數(shù)組的方法

    這篇文章主要介紹了C#使用foreach語句簡單遍歷數(shù)組的方法,涉及C#中foreach語句的使用技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • 解析C#面向?qū)ο缶幊讨蟹椒ǎ╩ethod)的使用

    解析C#面向?qū)ο缶幊讨蟹椒ǎ╩ethod)的使用

    這篇文章主要介紹了解析C#面向?qū)ο缶幊讨蟹椒ǎ╩ethod)的使用,方法是面向?qū)ο缶幊陶Z言中的基本特性,需要的朋友可以參考下
    2016-01-01

最新評論

项城市| 顺平县| 酉阳| 宁德市| 夏津县| 赤水市| 永嘉县| 贞丰县| 河北区| 乐安县| 嘉祥县| 南涧| 左贡县| 温宿县| 仁寿县| 格尔木市| 陈巴尔虎旗| 镇雄县| 曲阳县| 福安市| 鸡西市| 离岛区| 京山县| 新田县| 永靖县| 竹北市| 安远县| 通榆县| 宝坻区| 孟津县| 茌平县| 平安县| 柳州市| 济宁市| 城固县| 如东县| 合江县| 图木舒克市| 沙田区| 侯马市| 肇源县|