快讯
- 装机圈新宠儿,微星 MAG B850M MORTAR Wi-Fi迫击炮主板评测
- 微星MPG Z890I EDGE TI WIFI 刀锋 钛评测:14 层 PCB 加持,同级之中强无敌!
- 打造低调而卓越的Ai PC,微星MEG Z890 ACE战神主板开箱
- 解密“星”制造,微星深圳恩斯迈工厂探秘之旅
- 游泳也需音乐相伴!韶音OpenSwim Pro评测
- 又一款轻量化电竞
- 元气满满的充电之旅!铂陆帝商超活动嗨翻周末
- 雷柏机甲编码主题系列警戒线S-07图赏:以机械美感诠释潮流新理念桌搭!
- 更适合新手体质的枪战游戏,《无畏契约》开战!
- 现代与传统的融合之旅:贝尔金笔记本扩展坞体验
- 航天品质下的极速充电体验——航嘉G65 GaN快速充电器评测
- 有颜有实力的外设谁能不爱?来看雷柏商超巡演
- 新潮外设引爆全场!雷柏联合PC打造潮品酷玩趴
- 幻彩绚丽,玩趣十足!雷柏V700DIY键盘图赏
- U皇就该配板皇,超频玩家现身说法教你选主板
- 13代酷睿的超频利器,有好板才有好性能
- 全新升级,雷柏V20S RGB光学游戏鼠标2023版详解
- 马斯克30亿放“烟花”,民航故事为何值钱?
- 让露营生活更精致!铂陆帝户外电源AC180评测
- 惠威音响体验:音响中的艺术品,拥有好听的灵魂
- 装机圈新宠儿,微星 MAG B850M MORTAR WiFi迫击炮主板评测31日
- 微星MPG Z890I EDGE TI WIFI 刀锋 钛评测:14 层 PCB 加持,同级之中强无敌!29日
- 打造低调而卓越的Ai PC,微星MEG Z890 ACE战神主板开箱10日
- 解密“星”制造,微星深圳恩斯迈工厂探秘之旅02日
- 游泳也需要音乐相伴!韶音新一代游泳耳机OpenSwim Pro评测12日
- 又一款轻量化电竞"神鼠"来袭!玄熊猫3395游戏鼠标今晚首发149元10日
- 元气满满的充电之旅!铂陆帝商超活动嗨翻周末27日
- 雷柏机甲编码主题系列警戒线S-07图赏:以机械美感诠释潮流新理念桌搭!24日
- 更适合新手体质的枪战游戏,《无畏契约》国服正式开战!20日
- 玩物近话论:现代科技与甘南秘境的融合之旅 贝尔金笔记本扩展坞体验14日
- 航天品质下的极速充电体验——航嘉G65 GaN快速充电器评测12日
- 有颜有实力的外设好物谁能不爱?雷柏点燃PCGROUP潮品商超巡演15日
- 新潮外设好物引爆全场!雷柏联合PCGROUP打造潮品酷玩趴15日
- 幻彩绚丽,玩趣十足!雷柏V700DIY热插拔机械键盘图赏10日
- U皇就该配板皇,超频玩家现身说法教你选主板26日
- 13代酷睿的超频利器,有好板才有好性能25日
- 全新升级 经典复刻 雷柏V20S RGB光学游戏鼠标2023版详解25日
- 马斯克30亿放“烟花”,民航故事为何值钱?23日
- 告别电量焦虑,让露营生活多一分精致!铂陆帝户外电源AC180开箱评测17日
- 惠威音响体验:音响中的艺术品,拥有好听的灵魂04日
用VisualC#.net完成一个时间提醒器
2004-02-14 09:34 出处:PConline 作者:eyoexply/CSDN 责任编辑:linjixiong
1回顶部 有些人一用起电脑就会忘记时间,所以我就想做一个小工具,能实现闹钟的功能。
首先,要设计用户界面,要简介易用,所以不必太多的东西,要两个TextBox,两个Button,还有几个用来显示文字提示Lable,的就可以了!
把控件安排好以后,就可以编写代码了。其中一个TextBox(代码中的textBox1)是要输入时间到了以后要显示的提示信息。另一个TextBox则是设置时间的(代码中的textBox2)。设置时间的TextBox的格式是“00:00:00”,所以要有很多限定,比如只能输入数字,而且其中的两个冒号不能被修改。一下我就设计了SimpleTextBox类,来限制时间的输入。SimpleTextBox类代码如下:
文件:SimpleTextBox.cs
usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Drawing;
usingSystem.Data;
usingSystem.Windows.Forms;
namespaceSimpleTextBox
{
publicclassSimpleTextBox:System.Windows.Forms.TextBox
{
privatestringm_format;//设定时间的显示格式
privatecharm_inpChar;//缺省的字符
publicSimpleTextBox()
{
2回顶部
base.Multiline=false;
base.MaxLength=8;
m_inpChar='0';
m_format="00:00:00";
}
privateboolIsValidChar(charinput)
{
return(char.IsDigit(input));//检查是否为数字
}
//重载TextBox的OnKeyPress方法
protectedoverridevoidOnKeyPress(KeyPressEventArgse)
{
intstrt=base.SelectionStart;
intlen=base.SelectionLength;
intp;
//处理Backspace键->用缺省字符代替删除后的地方
if(e.KeyChar==0x08)
{
strings=base.Text;
p=Prev(strt);
if(p!=strt)
{
3回顶部
base.Text=s.Substring(0,p)+m_inpChar.ToString()+s.Substring(p+1);
base.SelectionStart=p;
base.SelectionLength=1;
}
e.Handled=true;
return;
}
//初始显示
if(m_format[strt]!=m_inpChar)
{
strt=Next(-1);
len=1;
}
//接受键盘的输入
if(IsValidChar(e.KeyChar))
{
stringt="";
t=base.Text.Substring(0,strt);
t+=e.KeyChar.ToString();
if(strt+len!=base.MaxLength)
{
t+=m_format.Substring(strt+1,len-1);
t+=base.Text.Substring(strt+len);
}
else
t+=m_format.Substring(strt+1);
base.Text=t;
//下一个输入字符
strt=Next(strt);
base.SelectionStart=strt;
//m_caret=strt;
base.SelectionLength=1;
}
e.Handled=true;
}
//将光标向前检测
privateintPrev(intstartPos)
{
intstrt=startPos;
intret=strt;
while(strt>0)
{
strt--;
if(m_format[strt]==m_inpChar)
returnstrt;
}
returnret;
}
4回顶部
//将光标向后检测,返回下一个字符的位置
privateintNext(intstartPos)
{
intstrt=startPos;
intret=strt;
while(strt
5回顶部
privateintclockTime=0;
privateintalarmTime=0;
privatestringmessage="时间到了";
privateSystem.Timers.TimertimerClock=newSystem.Timers.Timer();
publicintAlarmTime
{
set
{
alarmTime=value;
}
}
publicintClockTime
{
set
{
clockTime=value;
}
}
publicstringMessage
{
set
{
message=value;
}
}
publicintCountdown
{
get
{
returnalarmTime-clockTime;
}
}
publicTimerAlarm()
{
//MessageBox.Show("TimeAlarmstart.");
timerClock.Elapsed+=newElapsedEventHandler(OnTimer);
timerClock.Interval=1000;
timerClock.Enabled=true;
}
publicvoidOnTimer(Objectsource,ElapsedEventArgse)
{
try
{
clockTime++;
if(clockTime==alarmTime)
{
MessageBox.Show(message,"时间到了",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
6回顶部
catch(Exceptionex)
{
MessageBox.Show("OnTimer():"+ex.Message);
}
}
publicvoidStopTimer()
{
timerClock.Enabled=false;
}
}
然后用了FormatConvert类,它提供了两个静态的方法,inputToSeconds()将一个string型的时间字串转换成一共有多少秒。
publicstaticintinputToSeconds(stringtimerInput)
{
string[]timeArray=newstring[3];
intminutes=0;
inthours=0;
intseconds=0;
intoccurence=0;
intlength=0;
inttotalTime=0;
occurence=timerInput.LastIndexOf(":");
length=timerInput.Length;
//Checkforinvalidinput
if(occurence==-1||length!=8)
{
MessageBox.Show("InvalidTimeFormat.");
}
else
{
timeArray=timerInput.Split(':');
seconds=Convert.ToInt32(timeArray[2]);
minutes=Convert.ToInt32(timeArray[1]);
hours=Convert.ToInt32(timeArray[0]);
totalTime+=seconds;
totalTime+=minutes*60;
totalTime+=(hours*60)*60;
}
returntotalTime;
}
secondsToTime方法是把秒转换一个时间格式的字串返回。
publicstaticstringsecondsToTime(intseconds)
{
intminutes=0;
inthours=0;
while(seconds>=60)
{
minutes+=1;
seconds-=60;
}
while(minutes>=60)
{
hours+=1;
minutes-=60;
}
7回顶部
stringstrHours=hours.ToString();
stringstrMinutes=minutes.ToString();
stringstrSeconds=seconds.ToString();
if(strHours.Length<2)
strHours="0"+strHours;
if(strMinutes.Length<2)
strMinutes="0"+strMinutes;
if(strSeconds.Length<2)
strSeconds="0"+strSeconds;
returnstrHours+":"+strMinutes+":"+strSeconds;
}
下面就是主窗体了,分别编写两个按钮的事件:
开始按钮:
privatevoidbutton1_Click(objectsender,System.EventArgse)
{
timeAlarm=newTimerAlarm();
timeAlarm.AlarmTime=FormatConvert.inputToSeconds(this.textBox2.Text);
timeAlarm.Message=this.textBox1.Text;
if(timeAlarm.Countdown>0
this.timer1.Enabled=true;
if(textBox2.Text!="00:00:00")
this.textBox2.ReadOnly=true;
}
建立一个TimerAlarm的实例,开始计时。
重设按钮:
privatevoidbutton2_Click(objectsender,System.EventArgse)
{
this.textBox1.Clear();
this.timer1.Enabled=false;
if(timeAlarm!=null)
timeAlarm.StopTimer();
this.textBox2.ReadOnly=false;
this.textBox2.Text="00:00:00";
}
恢复程序。
还有一个Timer,用来动态显示剩下的时间。
privatevoidtimer1_Tick(objectsender,System.EventArgse)
{
if(timeAlarm.Countdown>=0)
textBox2.Text=FormatConvert.secondsToTime(timeAlarm.Countdown);
else
{
this.timer1.Enabled=false;
this.textBox2.ReadOnly=false;
}
}
程序基本上已经完成了。有兴趣的朋友还可以加入系统托盘等的功能。
第一次写这种文章,时间也比较仓促,希望大家多提意见。
(参考资料:C#MaskedEditControl----OscarBowyer,Useatimertocreateasimplealarmapplication----AndrewBoisen,fromCodiProject.Com)
|
最热搜索
无线路由器怎么用 会说话的汤姆猫电脑版 12306网上订火车票 跳舞吧 flash player 下载 PP助手电脑版 Adobe Reader(pdf阅读) iOS6正式版12项新功能 QQ空间克隆器 9158视频KTV 植物大战僵尸辅助工具 Win8怎么关机 QQ空间进不去 2013年春节是几月几号 QQ昵称 QQ空间皮肤 PPT模板 电脑输入法不见了怎么办 2012中秋节是几月几日 word安全模式 qq输入法怎么点亮 IE修复 感恩节是几月几日 CSS布局 PS快捷键 Outlook设置 声卡驱动器官方免费下载 格式工厂怎么用 桌面图标有阴影怎么去掉 Windows RT是什么意思 2013年日历设计 Word打不开怎么办 Win8专区 腾讯微云网 windows8激活工具 剑灵什么时候公测 QQ通讯录怎么用 开心斗地主 拖拉机小游戏 麻将游戏 中国象棋 德州扑克 黄金矿工中文版 保皇扑克游戏 四国军棋 万圣节是几月几日 ps抠图教程 12306订票助手 万圣节小游戏大全 rar文件怎么打开 Photoshop CS6教程 iOS6完美越狱 QQ个性签名