1、环境搭建和工程创建:
步骤一:安装visual studio2022
步骤二:新建工程
2、制作窗体部件
*踩过的坑:
(1)找不到工具箱控件,现象如下:
解决办法:
依次点击:工具栏->获取工具和功能->单个组件:安装3.5版本开发工具
若上述
办法不生效,继续检查.net桌面开发和asp.net开发是否勾选
最后点击顶部栏的:视图->工具箱就能显示出工具栏
3、界面布局设计
(1)界面设计如下:
4、具体功能函数
using system; using system.windows.forms; using static system.windows.forms.visualstyles.visualstyleelement; namespace myproject01 { public partial class form1 : form { uint16 timer_value = 0; //定时值 uint16 timer_count = 0; //定时器计数值 byte timer_status = 0; //定时器状态 0--停止 1 -- 定时状态 2 --暂停状态 public form1() { initializecomponent(); } private void textbox1_textchanged(object sender, eventargs e) { } private void button1_click(object sender, eventargs e) { } private void label1_click(object sender, eventargs e) { } private void button2_click(object sender, eventargs e) { } private void flowlayoutpanel1_paint(object sender, painteventargs e) { } private void label1_click_1(object sender, eventargs e) { } private void label2_click(object sender, eventargs e) { } private void label4_click(object sender, eventargs e) { } private void form1_load(object sender, eventargs e) { byte i; for (i = 0; i < 60; i ) { //分钟和秒钟的组合框初始化 combobox1.items.add(i.tostring()); combobox2.items.add(i.tostring()); combobox1.text = "45"; //初始化为45分钟 combobox2.text = "0"; } } private void combobox1_selectedindexchanged(object sender, eventargs e) { } private void textbox1_textchanged_1(object sender, eventargs e) { textbox1.readonly = true; } private void button1_click_1(object sender, eventargs e) { //定时器状态机函数 switch (timer_status) { case 0: { //获取定时时间,分钟*60 秒钟 timer_value = convert.touint16(combobox1.text, 10); timer_value *= 60; timer_value = convert.touint16(combobox2.text, 10); if (timer_value > 0) { //开始定时任务 textbox1.text = timer_value.tostring() " 秒"; button1.text = "暂停计时"; button2.enabled = true; combobox1.enabled = false; //关闭时间选择 combobox2 .enabled = false; timer1.start(); timer_status = 1; } else { messagebox.show("定时时间不能为0,请重新输入", "警告"); // } //进度条初始化 progressbar1.value = 0; progressbar1.maximum = timer_value; break; } case 1: { timer1.stop(); timer_status = 2; button1.text = "继续计时"; break; } case 2: { timer1.start(); timer_status = 1; button1.text = "暂停计时"; break; } default: { break; } } } //定时按钮单击事件 private void timer1_tick(object sender, eventargs e) { timer_count ; textbox1.text = timer_value-timer_count " 秒"; //更新进度条 progressbar1.value = timer_count; if (timer_count == timer_value) { timer1.stop(); timer_count = 0; system.media.systemsounds.asterisk.play(); button1.text = "计时结束"; messagebox.show ("定时时间到","提示"); button1.text = "开始定时"; combobox1.enabled = true; //关闭时间选择 combobox2.enabled = true; combobox1.text = "45"; //初始化为45分钟 combobox2.text = "0"; button2.enabled = false; timer_status = 0; progressbar1.value = 0; } } private void combobox2_selectedindexchanged(object sender, eventargs e) { } private void button2_click_1(object sender, eventargs e) { if(timer_status > 0) { timer_value = 0; //定时值 timer_count = 0; timer_status = 0; progressbar1.value = 0; textbox1.text= "0"; } timer1.stop(); timer_count = 0; button1.text = "开始定时"; combobox1.enabled = true; //关闭时间选择 combobox2.enabled = true; combobox1.text = "45"; //初始化为45分钟 combobox2.text = "0"; button2.enabled = false; timer_status = 0; timer_value = 0; } } }
到此这篇关于c#桌面应用开发实现番茄定时器的文章就介绍到这了,更多相关c# 番茄定时器内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!