国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

使用C#編寫一個計時器

2019-11-18 19:29:32
字體:
來源:轉載
供稿:網友

大家好!好久沒有上來了!小生在此向各方豪杰問好!
今天溫習了一下C#,解決了老大一個難題,以前總是搞不懂重載運算符,也不知道它有什么用處,
今天總算把它給用上了,真有種在深山老林里用上電得感覺,呵呵!我用它做了一個計時器,下面
我就把代碼帖出來供大家玩樂一下也好!先把步驟說一下:
1.用.net建立一個Windows應用程序
2.在項目下添加一個類命名為“Time“
3.編寫“Time“類得實現代碼
4.在窗體中放兩個Label控件和一個時鐘(Timer)控件
5.編寫窗體得Load事件和(Timer)控件的Tick事件

以下是“Time“類得實現代碼:
public class Time
 {
  PRivate int hours;  //小時
  private int minutes; //分鐘
  private int seconds; //秒鐘
 
  public Time()
  {
   this.hours = 0;
   this.minutes = 0;
   this.seconds = 0;
  }

  public Time(int hours,int minutes,int seconds)
  {
   this.hours = hours;
   this.minutes = minutes;
   this.seconds = seconds;
  }

  public void SetHours(int hours)
  {
   this.hours = hours;
  }

  public void SetMinutes(int minutes)
  {
   this.minutes = minutes;
  }

  public void SetSeconds(int seconds)
  {
   this.seconds = seconds;
  }

  public int GetHours()
  {
   return this.hours;
  }

  public int GetMinutes()
  {
   return this.minutes;
  }

  public int GetSeconds()
  {
   return this.seconds;
  }

  public static Time Operator ++(Time time)
  {
   time.seconds++;
   if (time.seconds >= 60)
   {
    time.minutes++;
    time.seconds = 0;
    if (time.minutes >= 60)
    {
     time.hours++;
     time.minutes = 0;
     time.seconds = 0;
     if (time.hours >= 24)
     {
      time.hours = 0;
      time.minutes = 0;
      time.seconds = 0;
     }
    }
   }
   return new Time(time.hours,time.minutes,time.seconds);
  }
 }

以下是Load事件和Timer控件的Tick事件處理程序:
private void Form1_Load(object sender, System.EventArgs e)
  {
   DateTime dtime = DateTime.Now;
   int hour = dtime.Hour;
   int minute = dtime.Minute;
   int second = dtime.Second;
   time = new Time(hour,minute,second);
   this.label1.Text = "當前時間是:" + hour + ":" + minute + ":" + second;
   this.timer1.Start();
  }
private void timer1_Tick(object sender, System.EventArgs e)
  {
   time++;
   this.label2.Text = "時間在流逝:" + time.GetHours() + ":" + time.GetMinutes() + ":" + time.GetSeconds();
  }

時間有限不說了,運行一下看看!

出處:風帆的Blog!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌拉特后旗| 专栏| 中卫市| 竹溪县| 桂东县| 龙门县| 奉贤区| 新宁县| 西藏| 长汀县| 资阳市| 清丰县| 新乐市| 赣榆县| 潍坊市| 蓝山县| 阳高县| 莲花县| 芮城县| 晋州市| 抚州市| 连州市| 兴隆县| 荔浦县| 甘洛县| 苏州市| 新竹市| 大方县| 天柱县| 天祝| 乾安县| 东山县| 中卫市| 渑池县| 横山县| 广灵县| 翁源县| 重庆市| 合江县| SHOW| 偃师市|