【问题标题】:How to use progressbar.value property at different locations of program如何在程序的不同位置使用progressbar.value 属性
【发布时间】:2017-06-02 15:34:44
【问题描述】:

我需要在不同的位置使用 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”一次或多次吗?

【问题讨论】:

  • 所以你想在每次点击按钮时更新 Progressbar 值,不是吗?>
  • 我在帖子中做了一些更新,请看一下
  • 不,不是在每个按钮单击中......,我应该能够在不同的位置使用“progressBar1.value”。这样,在每组数学计算之后,我就可以向我的客户展示发生了多少 % 的进度......
  • 在我创建此评论时,您的函数 button1_click 首先更新属于值 25 的所有元素。而不是返回,您继续将元素更新为值 75。当然,您以75% 的价值。建议:在第一次单击按钮、第二次、第三次等按钮单击时,清楚地为自己指定一个要求。之后 button_click 函数将相当简单

标签: c# winforms progress-bar


【解决方案1】:

试试这个,在windows窗体中拖放后台worker

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

【讨论】:

  • 谢谢..我希望“progressBar1.Value”没有for循环,这样我就可以根据自己的意愿给出尽可能多的“progressBar1.Value”..(我认为是不可能的,截至目前..因为我尝试了很多。“progressBar1.Value”只取程序中给出的最大值)
【解决方案2】:

使用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();
    }
}

【讨论】:

  • 我应该能够在不同的位置使用“progressBar1.value”。因此,在每组数学计算之后,我可以向我的客户展示,每时每刻发生了多少 % 的进步......
【解决方案3】:

在按钮点击中更新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;
     }
}

【讨论】:

  • 每次我需要点击这里的按钮。我想自动使用一些延迟,它应该是 50%/70%/ 或任何其他
【解决方案4】:

您需要在更改之间添加延迟。到现在为止,按钮将条形推进到 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
    }
}

【讨论】:

  • 这将冻结应用程序 UI
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-04
  • 2013-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多