如何将进度条与计时器刻度线同步 [英] How to Sync Progressbar with timer tick

查看:107
本文介绍了如何将进度条与计时器刻度线同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Winapp.

启动计时器1的按钮.

一个与计时器刻度线同步的progressBar1.

停止计时器的按钮.

一个listBox1,它显示检查计时器工作的当前日期和时间.

这是代码.

I am building an winapp.

A button which starts the timer1.

A progressBar1 which syncs with timer tick.

A button which stops the timer.

A listBox1 which shows the current date and time to check the timer working.

Here is the code.

private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Interval = 2000;
            this.listBox1.Items.Clear();
            progressBar1.Maximum = 10;
            timer1.Tick += new EventHandler(timer1_Tick);
        }





private void timer1_Tick(object sender, EventArgs e)
        {

            if (progressBar1.Value != 10)
            {
                progressBar1.Value++;
                this.listBox1.Items.Add("Event raised at :" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:nnn"));
            }

        }




上面的代码运行良好,但是progressBar1进度和计时器仅在一个循环后停止.

我希望计时器仅在用户需要时停止,并且progressBar1必须仅通过计时器停止.如果progressBar1已满,则必须再次启动.




The above code works well but the progressBar1 progress and timer stops after one loop only.

I want the timer to stop only when user wants and progressBar1 must stop only with timer.If progressBar1 is full it must start again.

推荐答案

请参见以下示例:

please see the following example:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.InitOthers();
        }

        // set initial properties
        private void InitOthers()
        {
            this.timer1.Tick += timer1_Tick;
            this.timer1.Interval = 1000;
            this.progressBar1.Maximum = 10;
            this.button1.Click += button1_Click;
        }

        // button click handler
        void button1_Click(object sender, EventArgs e)
        {
            // clear listbox items
            this.listBox1.Items.Clear();
            // reset timer
            this.timer1.Enabled = false;
            this.timer1.Enabled = true;
            // reset progress bar
            this.progressBar1.Value = 0;
        }

        // timer interval elapsed handler
        void timer1_Tick(object sender, EventArgs e)
        {
            if (this.progressBar1.Value < 10)
                this.progressBar1.Value++; // progress
            else
                this.timer1.Enabled = false; // stop timer

            this.listBox1.Items.Insert(0,
                string.Format("tick: {0}", DateTime.Now.ToString("HH:mm:ss.fff")));
        }
    }




您的代码有几点:
1-(在button1_Click中)每次按下button1时都会添加刻度处理程序,这意味着刻度处理程序将在第一次button1单击时运行一次,在第二次单击时运行两次,依此类推.我不确定这是您想要的. > 2-(在timer1_Click中)处理不等于10"的情况.如果相等"呢?在处理任何"if"时,最好考虑"else"部分.当然,您不必,有时也不需要,但是我建议您认为"if"及其"else"部分.
3-(int button1_Click)``timer1.Enable = true''不会重置timer1.如果已启用,它将根据开始时间继续滴答.如果要重置它,则必须先停止"然后再次开始".
4-(在timer1_Tick中)日期时间的自定义格式字符串有错误.没有格式说明符,例如"nnn".

最重要的一点实际上是在评论中提到的.使用调试器查看会发生什么情况.




There are some points with your code:
1- (in button1_Click) You add tick handler every time when you press button1 which means tick handler will run once for the first button1 click, twice for the second click, etc. I am not sure that is what you want.
2- (in timer1_Click) You handle the case of ''not equal to 10''. What about if ''equal''? It is good practice to think about ''else'' part when you handle any ''if''. Of course, you don''t have to and sometimes you don''t need to, but I advice you think ''if'' with its ''else'' part.
3- (int button1_Click) ''timer1.Enable=true'' doesn''t reset timer1. If it is already enabled, it continues to tick according to start time. If you want to reset it, you have to ''stop'' then ''start'' again.
4- (in timer1_Tick) Your custom format string for datetime has an error. no format specifier exists like ''nnn''.

the most important point is actually mentioned in comments. use your debugger to see what happens.


如果您需要在单击开始"按钮时显示进度动画,而在单击停止"按钮时需要停止进度动画,则只需执行以下操作

开始按钮单击
if you need to show the progress animation when you click start button and need to stop it when you click stop button you can simply do as below

start button click
progressBar1.Style = ProgressBarStyle.Marquee;
progressBar1.MarqueeAnimationSpeed = 30;


停止按钮单击


stop button click

progressBar1.Style = ProgressBarStyle.Continuous;
progressBar1.MarqueeAnimationSpeed = 0;


这对我有用吗

Here Is What Worked For me

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

namespace TImer_Sync_WIth_ProGressbar
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Interval = 2000;
            this.listBox1.Items.Clear();
            progressBar1.Maximum = 10;
            progressBar1.Maximum = 100;
            timer1.Tick += new EventHandler(timer1_Tick);
        }



        private void progressBar1_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (progressBar1.Value == 100)
            {
                progressBar1.Value = 10;
            }
            else
            {
                progressBar1.Value++;
                this.listBox1.Items.Add("Event raised at :" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:nnn"));
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }

        
    }
}


这篇关于如何将进度条与计时器刻度线同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆