C#倒计时应用(设计练习1)

目的:实现秒的倒计时功能

目录

1、Form窗体设计

2、设计思路

3、源码如下


1、Form窗体设计

整体展示:

 定时时间:ComboBox        剩余时间:Lable        进度条:ProgerssBar        计时器:Timer

开始计时、暂停计时、停止计时:Button

2、设计思路

        1).ComboBox下拉框的数据填充。通过for循环0-100的整数来填充下拉框。

        2).倒计时的原理。通过设定的定时时间-计时器的计时=剩余时间。

        3).Button的禁用和启用以及其他参数的初始化。 

                 a、初始化三个按钮都是禁用状态,ComboBox为未选中状态。ProgerssBar进度条为最大位置。

                b、当定时时间为未选中时,开始计时按钮是禁用状态。

                c、当剩余时间Lable没有数值时,暂停和停止计时是禁用状态。

        4).开始计时:a、点击开始计时,将定时时间的数值赋给剩余时间。

                              b、将定时时间的值赋给ProgerssBar进度条的最大值。

                              c、启动计时器。

        5).定时器事件:a、剩余时间=将计时器的数值-定时时间。

                                  b、ProgerssBar进度条的值=进度条最大值-计时器数值。

                                  c、当计时器数值=定时时间时,计时器计数清零、弹窗提醒倒计时结束、计时器停止。

        6).暂停计时:停止计时器计时。再次点击开始计时,继续暂停前的倒计时。

        7).停止计时:计时器清零、定时时间恢复到未选中的状态、剩余时间为空、进度条为最大 值、计时器停止工作。

3、源码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace timer_Dmeo
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
            //初始化
            cbo_TimeSet.SelectedIndex = -1;
            btn_Start.Enabled = false;
            btn_Stop.Enabled = false;
            btn_Suspend.Enabled = false;
            pro_Time.Value = pro_Time.Maximum;
        }
        int count; //系统定时器计数
        int time;  //定时时间
        private void Form1_Load(object sender, EventArgs e)//窗体创建后运行的函数(Load)
        {
            for (int i = 0; i < 100; i++)    
            {
                cbo_TimeSet.Items.Add(i.ToString()+"秒");//向下拉框添加数据0-100
            }
        }
        //计时器
        private void timer1_Tick(object sender, EventArgs e)
        {
            
            count++;//自加1
            lbl_Surplus.Text = ((time - count)*0.1).ToString()+"秒"; //剩余时间开始显示
            pro_Time.Value = pro_Time.Maximum-count;
            if (count == time)
            {
                btn_Stop_Click(null,null);
                timer1.Stop();  //停止计时
              
                count = 0;
                MessageBox.Show("倒计时结束!");
            }
        }
        //开始计时
        private void btn_Start_Click(object sender, EventArgs e)
        {
            string temp = cbo_TimeSet.Text.Trim(); //将comBox选择的数据赋值给剩余时间
            time = Convert.ToInt16(temp.Substring(0,2))*10;
            pro_Time.Maximum = time;
            timer1.Start();//计时器开始计时
        }
        //暂停计时
        private void btn_Suspend_Click(object sender, EventArgs e)
        {
            timer1.Stop();//停止计时器
        }
        //停止计时
        private void btn_Stop_Click(object sender, EventArgs e)
        {
            count = 0;//计时器计数清零
            this.cbo_TimeSet.SelectedIndex = -1;  //设定时间恢复到未选择状态
            lbl_Surplus.Text = string.Empty;  //倒计时恢复空值
            pro_Time.Value = pro_Time.Maximum; 
            timer1.Stop();  //计时器停止工作
        }

        #region Button按钮的禁用
        private void lbl_Surplus_TextChanged(object sender, EventArgs e)
        {
            if (lbl_Surplus.Text.Length == 0)
            {
                btn_Suspend.Enabled = false; btn_Stop.Enabled = false;
            }
            else
            {
                btn_Suspend.Enabled = true; btn_Stop.Enabled = true;
            }
        }
        private void cbo_TimeSet_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbo_TimeSet.SelectedIndex == -1)
            {
                btn_Start.Enabled = false;
            }
            else
            { btn_Start.Enabled = true; }
        }
        #endregion
    }
}

以下是一个简单的C#打字练习软件设计: 1. 首先,需要创建一个Windows窗体应用程序。 2. 在窗体中添加一个文本框和一个标签,用于显示练习文本和用户输入的文本。 3. 在窗体中添加一个计时器控件,用于计算用户完成练习所需的时间。 4. 在窗体中添加一个按钮,用于开始练习。 5. 在代码中,需要定义一个字符串数组,用于存储练习文本。 6. 在按钮的Click事件中,随机选择一个字符串数组中的文本,并将其显示在标签中。 7. 在文本框的TextChanged事件中,将用户输入的文本与练习文本进行比较,并将正确的字符显示为绿色,错误的字符显示为红色。 8. 在计时器的Tick事件中,更新计时器的显示,并检查用户是否完成了练习。 9. 如果用户完成了练习,停止计时器并显示完成时间和准确性。 以下是示例代码: ```csharp public partial class Form1 : Form { private string[] texts = { "The quick brown fox jumps over the lazy dog.", "Sphinx of black quartz, judge my vow.", "How vexingly quick daft zebras jump!", "Pack my box with five dozen liquor jugs." }; private string currentText; private int currentIndex; private int correctCount; private int errorCount; private bool isStarted; public Form1() { InitializeComponent(); } private void btnStart_Click(object sender, EventArgs e) { if (!isStarted) { currentIndex = new Random().Next(texts.Length); currentText = texts[currentIndex]; lblText.Text = currentText; txtInput.Text = ""; correctCount = 0; errorCount = 0; lblAccuracy.Text = ""; lblTime.Text = "00:00:00"; timer1.Start(); isStarted = true; } } private void txtInput_TextChanged(object sender, EventArgs e) { if (isStarted) { string inputText = txtInput.Text; int minLength = Math.Min(currentText.Length, inputText.Length); for (int i = 0; i < minLength; i++) { if (inputText[i] == currentText[i]) { txtInput.Select(i, 1); txtInput.SelectionColor = Color.Green; } else { txtInput.Select(i, 1); txtInput.SelectionColor = Color.Red; errorCount++; } } if (inputText.Length > currentText.Length) { txtInput.Select(currentText.Length, inputText.Length - currentText.Length); txtInput.SelectionColor = Color.Red; errorCount += inputText.Length - currentText.Length; } lblAccuracy.Text = $"{100 * correctCount / (correctCount + errorCount)}%"; } } private void timer1_Tick(object sender, EventArgs e) { TimeSpan timeSpan = TimeSpan.FromSeconds(timer1.Interval / 1000 + timer1.Tag == null ? 0 : (int)timer1.Tag); lblTime.Text = timeSpan.ToString("hh\\:mm\\:ss"); if (isStarted && txtInput.Text == currentText) { timer1.Stop(); MessageBox.Show($"Congratulations! You finished the text in {timeSpan.TotalSeconds} seconds with {lblAccuracy.Text} accuracy."); isStarted = false; } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值