C#实现闹钟AlarmClock实例代码
程序员文章站
2024-02-17 08:40:34
本文所述是一个小巧且功能简单的c#闹钟程序代码alarmclock,程序涉及到了axinterop.msmask.dll与interop.msmask.dll两个控件,对编...
本文所述是一个小巧且功能简单的c#闹钟程序代码alarmclock,程序涉及到了axinterop.msmask.dll与interop.msmask.dll两个控件,对编写时间闹钟或许有点参考价值。
完整实例代码如下:
using system; using system.drawing; using system.collections; using system.componentmodel; using system.windows.forms; using system.data; namespace alarmclock { /// <summary> /// form1 的摘要说明。 /// </summary> public class form1 : system.windows.forms.form { private system.windows.forms.monthcalendar monthcalendar1; private system.windows.forms.label label1; private system.windows.forms.textbox textbox1; private system.windows.forms.listbox listbox1; private axmsmask.axmaskedbox mask1; private system.windows.forms.timer timer1; private system.windows.forms.button buttonadd; private system.windows.forms.button buttondel; private system.componentmodel.icontainer components; public form1() { initializecomponent(); } protected override void dispose( bool disposing ) { if( disposing ) { if (components != null) { components.dispose(); } } base.dispose( disposing ); } #region windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void initializecomponent() { this.components = new system.componentmodel.container(); system.resources.resourcemanager resources = new system.resources.resourcemanager(typeof(form1)); this.monthcalendar1 = new system.windows.forms.monthcalendar(); this.label1 = new system.windows.forms.label(); this.mask1 = new axmsmask.axmaskedbox(); this.textbox1 = new system.windows.forms.textbox(); this.listbox1 = new system.windows.forms.listbox(); this.buttonadd = new system.windows.forms.button(); this.buttondel = new system.windows.forms.button(); this.timer1 = new system.windows.forms.timer(this.components); ((system.componentmodel.isupportinitialize)(this.mask1)).begininit(); this.suspendlayout(); // // monthcalendar1 // this.monthcalendar1.location = new system.drawing.point(12, 112); this.monthcalendar1.name = "monthcalendar1"; this.monthcalendar1.tabindex = 0; // // label1 // this.label1.font = new system.drawing.font("宋体", 14.25f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134))); this.label1.location = new system.drawing.point(8, 16); this.label1.name = "label1"; this.label1.size = new system.drawing.size(88, 23); this.label1.tabindex = 1; this.label1.text = "label1"; // // mask1 // this.mask1.location = new system.drawing.point(16, 48); this.mask1.name = "mask1"; this.mask1.ocxstate = ((system.windows.forms.axhost.state)(resources.getobject("mask1.ocxstate"))); this.mask1.size = new system.drawing.size(88, 23); this.mask1.tabindex = 2; // // textbox1 // this.textbox1.location = new system.drawing.point(16, 80); this.textbox1.name = "textbox1"; this.textbox1.size = new system.drawing.size(88, 21); this.textbox1.tabindex = 3; this.textbox1.text = "输入内容"; // // listbox1 // this.listbox1.itemheight = 12; this.listbox1.location = new system.drawing.point(120, 16); this.listbox1.name = "listbox1"; this.listbox1.size = new system.drawing.size(152, 52); this.listbox1.tabindex = 4; // // buttonadd // this.buttonadd.location = new system.drawing.point(120, 80); this.buttonadd.name = "buttonadd"; this.buttonadd.size = new system.drawing.size(56, 24); this.buttonadd.tabindex = 5; this.buttonadd.text = "添加"; this.buttonadd.click += new system.eventhandler(this.buttonadd_click); // // buttondel // this.buttondel.location = new system.drawing.point(216, 80); this.buttondel.name = "buttondel"; this.buttondel.size = new system.drawing.size(56, 24); this.buttondel.tabindex = 6; this.buttondel.text = "删除"; this.buttondel.click += new system.eventhandler(this.buttondel_click); // // timer1 // this.timer1.enabled = true; this.timer1.interval = 1000; this.timer1.tick += new system.eventhandler(this.timer1_tick); // // form1 // this.autoscalebasesize = new system.drawing.size(6, 14); this.clientsize = new system.drawing.size(292, 266); this.controls.add(this.buttondel); this.controls.add(this.buttonadd); this.controls.add(this.listbox1); this.controls.add(this.textbox1); this.controls.add(this.mask1); this.controls.add(this.label1); this.controls.add(this.monthcalendar1); this.name = "form1"; this.text = "小闹钟"; this.load += new system.eventhandler(this.form1_load); ((system.componentmodel.isupportinitialize)(this.mask1)).endinit(); this.resumelayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [stathread] static void main() { application.run(new form1()); } private void timer1_tick(object sender, system.eventargs e) { //计时事件 label1.text = system.datetime.now.tolongtimestring(); //定时功能 int i; //当前时间信息 int hh = datetime.now.hour; int mm = datetime.now.minute; int ss = datetime.now.second; for( i = 1; i <= listbox1.items.count; i++ ) { string mytime; int myhh, mymm, myss; //获取事件字符串 mytime = listbox1.items[i-1].tostring( ); myhh = int.parse( mytime.substring(0,2) ); if( hh == myhh ) { mymm = int.parse( mytime.substring(2,2) ); if( mm == mymm ) { myss = int.parse( mytime.substring(4,2) ); if( ss == myss ) { string tempmsg = mytime.substring(7, mytime.length - 7 ); messagebox.show( tempmsg, "闹钟提示" ); } } } } } private void buttonadd_click(object sender, system.eventargs e) { string tempstr; int tss; int tmm; int thh; //获取用户输入的时间 tempstr = mask1.cliptext; //判断输入是否完整 if( tempstr.length < 6 ) { messagebox.show( "输入格式错误,不足位请补0", "格式错误" ); //设置焦点 mask1.focus( ); //设置重新输入起始位置 mask1.selstart = 0; //设置选择字符的长度 mask1.sellength = 8; return; } //取得小时数并加以判断 thh = int.parse( tempstr.substring(0,2) ); if( thh < 0 || thh > 23 ) { messagebox.show( "输入小时数应在0到23之间", "小时数错误" ); mask1.focus( ); mask1.selstart = 0; mask1.sellength = 2; return; } //获取分钟数并加以判断 tmm = int.parse( tempstr.substring(2,2) ); if( tmm < 0 || tmm > 60 ) { messagebox.show( "输入分钟数应在0到60之间", "分钟数错误" ); mask1.focus( ); mask1.selstart = 3; mask1.sellength = 2; return; } //获取秒钟数并加以判断 tss = int.parse( tempstr.substring(4,2) ); if( tss < 0 || tss > 60 ) { messagebox.show( "输入秒钟数应在0到60之间", "秒钟数错误" ); mask1.focus( ); mask1.selstart = 6; mask1.sellength = 2; return; } //将定时事件添加到listbox中 tempstr = tempstr + "-" + textbox1.text; listbox1.items.add( tempstr ); textbox1.text = ""; } private void buttondel_click(object sender, system.eventargs e) { //如果列表中存在定时项 if( listbox1.items.count > 0 ) { //删除选中的项 if( listbox1.selecteditems.count > 0 ) { listbox1.items.removeat( listbox1.selectedindex ); } } } private void form1_load(object sender, system.eventargs e) { label1.text = system.datetime.now.tolongtimestring(); } } }
本文实例代码备有较为详尽的注释,可以帮助大家很好的理解代码段的功能,希望本文所述实例对大家的c#程序设计有一定的帮助作用。