C#桌面应用开发:番茄定时器

C#桌面应用开发:番茄定时器

1、环境搭建和工程创建:

步骤一:安装visual studio2022

步骤二:新建工程

2、制作窗体部件

*踩过的坑:

(1)找不到工具箱控件,现象如下:

解决办法:

依次点击:工具栏->获取工具和功能->单个组件:安装3.5版本开发工具

若上述

办法不生效,继续检查.NET桌面开发和ASP.NET开发是否勾选

最后点击顶部栏的:视图->工具箱就能显示出工具栏

3、界面布局设计

(1)界面设计如下:

4、具体功能函数

c# 复制代码
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;
        }
    }
}
相关推荐
ghost1434 小时前
C#学习第27天:时间和日期的处理
开发语言·学习·c#
jason成都4 小时前
c#压缩与解压缩-SharpCompress
开发语言·c#
傻啦嘿哟5 小时前
从零开始:用Tkinter打造你的第一个Python桌面应用
开发语言·c#
CodeCraft Studio6 小时前
PDF处理控件Aspose.PDF教程:在 C# 中更改 PDF 页面大小
前端·pdf·c#
InCerry7 小时前
.NET周刊【5月第4期 2025-05-25】
c#·.net·.net周刊
阿蒙Amon10 小时前
C#获取磁盘容量:代码实现与应用场景解析
开发语言·c#
界面开发小八哥10 小时前
VS代码生成工具ReSharper v2025.1——支持.NET 10和C# 14预览功能
开发语言·ide·c#·.net·visual studio·resharper
CN.LG11 小时前
C# 从 ConcurrentDictionary 中取出并移除第一个元素
java·开发语言·c#
碎碎念的安静12 小时前
WPF可拖拽ListView
c#·wpf
程序猿小D13 小时前
第12节 Node.js 函数
开发语言·ide·人工智能·vscode·node.js·c#·编辑器