如何在程序的不同位置使用progressbar.value属性

时间:2017-06-02 04:30:35

标签: c# winforms progress-bar

我需要在不同的位置使用progressbar.value属性。但问题是,执行它时只显示给定的最大值。我需要停止在25%和75%,经过一段时间的延迟,100%。我怎样才能克服这个问题。在此先感谢...

C#

namespace ProgressBarWindowForm
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, System.EventArgs e)
        {
            label1.Hide();
            progressBar1.Minimum = 0;
            progressBar1.Maximum = 100;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            progressBar1.Value = 25;

            if (progressBar1.Value == 25)
            {
                label1.Show();
                label1.Text = "Process Complete 25%";
            }
            progressBar1.Value = 75;

            if (progressBar1.Value == 75)
            {
                label1.Show();
                label1.Text = "Process Complete 75%";
            }
       }
   }
}

进度条控件名称为 progressBar1 , 标签名称为 label1 和 按钮名称为 button1 当我单击按钮时,进度条值直接填充75%。我想以25%停止它,经过一段时间的延迟它应该填满75%然后100%...任何人都可以帮助..我可以使用" progressBar1.value"我需要一次或多次?

My form looks like this

4 个答案:

答案 0 :(得分:1)

在按钮单击中更新progressBar值很简单,您可以在页面加载中初始化属性,或者使用设计器,在页面加载中它将如下所示:

private int ProgressPercentage = 10;
public void Form1_Load(object sender, System.EventArgs e)
{
     progressBar1.Minimum = 0;
     progressBar1.Maximum = 100;
     progressBar1.Value = 0;
}

所以初始化完成后,现在您可以像下面那样编写按钮点击代码,通过它可以在每次点击按钮时更新进度条:

private void button1_Click(object sender, EventArgs e)
{
     progressBar1.Value += ProgressPercentage;
     label1.Text = String.Format("Process Complete {0}%",progressBar1.Value);
}

如果您希望在特定时间间隔内自动更新,则意味着您可以使用计时器并在按钮单击中启用计时器。 Here你可以找到一个类似的线程,可以用来为你的场景实现计时器。

根据您的评论更新,调用延迟不是最佳做法,您可以在此处使用计时器,如下所示:

System.Windows.Forms.Timer proTimer = new System.Windows.Forms.Timer();
private void Form1_Load(object sender, EventArgs e)
{
    proTimer.Interval = 1000;
    progressBar1.Minimum = 0;
    progressBar1.Maximum = 100;
    progressBar1.Value = 0;
    proTimer.Tick += new EventHandler(proTimer_Tick);
}
private void button1_Click(object sender, EventArgs e)
{
    proTimer.Enabled = true;
    proTimer.Start();
}

// Timer event
void proTimer_Tick(object sender, EventArgs e)
{
     progressBar1.Value += ProgressPercentage;
     label1.Text = String.Format("Process Complete {0}%",progressBar1.Value);        
     if (progressBar1.Value == 100)
     {
        proTimer.Stop();
        proTimer.Enbled = false;
     }
}

答案 1 :(得分:1)

试试这个,在windows窗体中拖放背景工作者

 public partial class Form1 : Form
 {
   public Form1()
   {
    InitializeComponent();

    backgroundWorker1.WorkerReportsProgress = true;
    // This event will be raised on the worker thread when the worker starts
    backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
    // This event will be raised when we call ReportProgress
    backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
   }
  private void button1_Click(object sender, EventArgs e)
  {
    // Start the background worker
    backgroundWorker1.RunWorkerAsync();
  }
// On worker thread so do our thing!
  void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        // Your background task goes here
        for (int i = 0; i <= 100; i++)
        {
            // Report progress to 'UI' thread
            backgroundWorker1.ReportProgress(i);
            // Simulate long task
            if (label1.InvokeRequired)
            {
                label1.Invoke(new MethodInvoker(delegate
                {
                    label1.Show();
                    label1.Text = "Process Complete " + progressBar1.Value + "%";
                }));
            }
            if (progressBar1.Value == 25 || progressBar1.Value == 75)
            {
                System.Threading.Thread.Sleep(1000);
            }
            System.Threading.Thread.Sleep(100);
        }
    }
// Back on the 'UI' thread so we can update the progress bar
  void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
   {
    // The progress percentage is a property of e
    progressBar1.Value = e.ProgressPercentage;
   }
 }

答案 2 :(得分:1)

延迟后使用Timer更新进度条:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        timer.Tick += Timer_Tick;
        timer.Interval = 1000; // delay: 1000 milliseconds
    }

    Timer timer = new Timer();

    private void Timer_Tick(object sender, EventArgs e)
    {
        if (progressBar1.Value == 100)
        {
            timer.Stop();
            return;
        }
        progressBar1.Value += 25;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        progressBar1.Value = 25;
        timer.Start();
    }
}

答案 3 :(得分:0)

您需要在更改之间添加延迟。截至目前,按钮将条形推进至25,设置标签,然后将条形推进至75而不会暂停。

System.Threading.Thread.Sleep(n);将在n毫秒内休眠,在语句设置25%标记后您将需要它。

修改

如果您希望仅在按钮单击时进行该值,则需要先检查进度条的值。

在伪代码中,类似于:

onclick() {
    if (progress == 0) {
        progress = 25
        label = the25MarkText
    } else if (progress == 25) {
        progress = 75
        label = the75MarkText
    }
}