进度条C#倒计时倒计时 [英] Timer countdown with progress bar C#
本文介绍了进度条C#倒计时倒计时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮助我,
i希望我的项目中的进度条与计时器倒计时同步,
i想要的时候计时器是结束,进度条也结束了
这就是我现在拥有的,
我尝试过:
Please help me,
i want to sync my progress bar in my project with the timer countdown,
i want when the timer is end, the progress bar is finish too
this is what i has now,
What I have tried:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace Timer1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int counter=80;
DateTime dt = new DateTime();
private void button1_Click(object sender, EventArgs e)
{
int counter = 80;
timer1 = new System.Windows.Forms.Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000;
timer1.Start();
textBox1.Text = counter.ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
counter--;
progressBar1.Increment(1);
if (counter == 0)
{
timer1.Stop();
}
textBox1.Text = dt.AddSeconds(counter).ToString("mm:ss");
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
textBox1.Clear();
progressBar1.Value = 0;
}
}
}
推荐答案
试试我的代码......任何疑问都会问我在fb.com/syedmca
Just Try my code... any doubt ask me in fb.com/syedmca
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Start();
timer1.Interval = 100;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (label1.Text == "0")
{
timer1.Stop();
}
else
{
label1.Text = (Convert.ToInt32(label1.Text) - 1).ToString();
progressBar1.Value = 100-Convert.ToInt32(label1.Text);
}
}
}
这篇关于进度条C#倒计时倒计时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文