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

首頁 > 開發 > 綜合 > 正文

使用C#編寫一個計時器

2024-07-21 02:18:15
字體:
來源:轉載
供稿:網友
大家好!好久沒有上來了!小生在此向各方豪杰問好!
今天溫習了一下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();
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台江县| 勃利县| 英超| 马鞍山市| 江北区| 方城县| 谢通门县| 隆尧县| 固镇县| 阳曲县| 鸡东县| 上饶市| 罗田县| 宁远县| 繁昌县| 长寿区| 合作市| 乃东县| 沐川县| 光泽县| 穆棱市| 宝丰县| 合作市| 石景山区| 高邮市| 府谷县| 武强县| 孟州市| 大埔县| 西贡区| 富宁县| 营口市| 开鲁县| 阳西县| 礼泉县| 乡宁县| 肃宁县| 贵德县| 靖江市| 东海县| 望江县|