gpt4 book ai didi

c# - 带有进度条 C# 的计时器倒计时

转载 作者:行者123 更新时间:2023-11-30 23:28:03 25 4
gpt4 key购买 nike

我想将项目中的进度条与计时器倒计时同步。

这是我现在拥有的:

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;
}
}
}

我希望进度条在计时器结束时结束。

enter image description here

最佳答案

您需要指定 progressBar1 以使其具有 .StepMaximum 属性匹配 Intervalcounter 变量。例如:

    private int counter = 80;
DateTime dt = new DateTime();
private void button1_Click(object sender, EventArgs e)
{
// The max is the total number of iterations on the
// timer tick by the number interval.
progressBar1.Max = counter * 1000;
progressBar1.Step = 1000;

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--;

// Perform one step...
progressBar1.PerformStep();

if (counter == 0)
{
timer1.Stop();
}
textBox1.Text = dt.AddSeconds(counter).ToString("mm:ss");
}

关于c# - 带有进度条 C# 的计时器倒计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36184884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com