快讯

用VisualC#.net完成一个时间提醒器

2004-02-14 09:34  出处:PConline  作者:eyoexply/CSDN  责任编辑:linjixiong 

  有些人一用起电脑就会忘记时间,所以我就想做一个小工具,能实现闹钟的功能。   首先,要设计用户界面,要简介易用,所以不必太多的东西,要两个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()   {
察看评论详细内容 我要发表评论
作者笔名 简短内容 发表时间
:
  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)      {   
察看评论详细内容 我要发表评论
作者笔名 简短内容 发表时间
:
  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;      }
察看评论详细内容 我要发表评论
作者笔名 简短内容 发表时间
:
     //将光标向后检测,返回下一个字符的位置      privateintNext(intstartPos)      {      intstrt=startPos;      intret=strt;      while(strt 察看评论详细内容 我要发表评论
作者笔名 简短内容 发表时间
:
  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);      }        }   
察看评论详细内容 我要发表评论
作者笔名 简短内容 发表时间
:
  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;      }
察看评论详细内容 我要发表评论
作者笔名 简短内容 发表时间
:
  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)     
察看评论详细内容 我要发表评论
作者笔名 简短内容 发表时间
:
IT热词搜索 来源:360新闻
软件论坛帖子排行
相关文章

相关软件:

腾讯QQ2012
大小:52.93 MB 授权:免费
腾讯QQ2012
立即下载
腾讯QQ2013
大小:49.32 MB 授权:免费
腾讯QQ2013
立即下载