Melek Jaziri
Melek Jaziri

Reputation: 11

Winforms Progress Bar update

I'm a new programmer to C# and I would like some help. I searched a lot but I didn't find a simple example. Please see the code below:

 public partial class Welcome : Form
 {
     public Welcome()
     {
         InitializeComponent();
     }

     private void button3_Click(object sender, EventArgs e)
     {
         Compare comp = new Compare();
         comp.Comparator();
     }
 }

In the Compare Class I have a simple method that contains a simple loop:

 public class Compare
 {
     public void Comparator()
     {
         for (int i;i<val;i++)
         { /* ............. */ }
     }

 }

I want to update the ProgressBar in parallel with an increment of the value of i.

Upvotes: 0

Views: 1119

Answers (2)

Pedreiro
Pedreiro

Reputation: 1804

A simple way of doing that is:

     public class Compare
     {
         public void Comparator()
         {
             progressBar.Value = 0;
             progressBar.Maximum = val;
             progressBar.Step = 1;
             for (int i;i<val;i++)
             { 
                 /* ............. */ 
                 progressBar.PerformStep();
             }
         }
     }

Upvotes: 0

Peuczynski
Peuczynski

Reputation: 4733

You said you searched a lot... where? These are most trivial things with very well described examples in MSDN

in BackgroundWorker_doWork method:

Parallel.For(0, val, i =>
{
    ...
    backgroundWorker.ReportProgress(0);
});

in BackgroundWorker_reportProgress method:

wf.progressBar.Value=wf.progressBar.Value + 1;

in Main form constructor

public Welcome()
{
    InitializeComponent();
    Compare.wf=this;
}

in

public class Compare
{
    static Welcome wf;
    public void Comparator()
    {
        backgroundWorker.RunWorkerAsync();
    }
}

Upvotes: 1

Related Questions