C#进度栏和计时器控件 [英] C# Progressbar and Timer control
本文介绍了C#进度栏和计时器控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我正在编写一个需要显示进度条的应用程序,下面是代码示例.问题:百分比变量没有更新,它的值始终为零,为什么有什么主意?
Hi
I''m writing an application where i need to show a progressbar, below is a sample of the code. Problem: the percentage variable does not get updated, it''s value is always zero, any ideas why?
private void trTick_Tick(object sender, EventArgs e)
{
pbProg.Minimum = 0;
pbProg.Maximum = 100;
pbProg.Value = 1;
if (pbProg.Value == pbProg.Maximum)
{
//trTick.Stop();
trTick.Enabled = false;
pbProg.Value = 0;
pbProg.Enabled = false;
}
pbProg.Step = 1;
pbProg.PerformStep();
pbProg.Value++;
int percentage = (pbProg.Value / pbProg.Maximum) * 100;
lblProg.Text = "Current progress: " + percentage.ToString() + "%";
}
private void btnStart_Click(object sender, EventArgs e)
{
trTick.Enabled = true;
}
推荐答案
int percentage = (pbProg.Value / pbProg.Maximum) * 100;
您将在此处获得整数除法,因此第一部分最终为0.试试:
You''re getting integer division here, so the first part ends up as 0. Try:
int percentage = (pbProg.Value * 100) / pbProg.Maximum;
您必须调用pbProg.Update()
.
计时器不一定是必需的.您可以通过不同的方式来执行此操作.您可以使用for循环来增加进度条的值.
The timer is not necessarily required. You can do this in different ways. You can use a for loop to increase the value of the progress bar.
#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace Progress_Bar_Example
{
partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Set the Max. value the progress bar can hold to 100.
progressBar1.Maximum = 100;
}
private void button1_Click(object sender, EventArgs e)
{
int value;
for (value = 0; value != 1000; value++)
{
progressBar1.Value = progressBar1.Value + 1;
}
}
}
}
这可能不是完美的方法,但仍然可以正常工作.可能还有许多其他解决方案.让我知道这是否有帮助
-
AJ
This may not be the perfect way of doing it but still it works fine. There may be many other solutions. Let me know if this was helpful
--
AJ
这篇关于C#进度栏和计时器控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文