Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Entering 30 inside the textbox should complete the progress bar and entering 300 should complete the progress bar. my actual problem :- When entering 30, the progress bar is completed once and the progress starts again ,And the form should be closed when the progress bar is complete

What I have tried:

C#
 private void form_Load(object sender, EventArgs e)
        {
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.RunWorkerAsync();
        }


private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            int str = textbox1.text;
                for (int i = 0;i <= str; i++)
                {
                    //   updatepicbox();
                    backgroundWorker1.ReportProgress(i);
                    System.Threading.Thread.Sleep(100);
                }
        }


private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            updatepicbox();

            progressBar1.Value = e.ProgressPercentage;
            label1.Text = string.Format("{0}%", e.ProgressPercentage);
            progressBar1.Update();
        }


private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Hide();
        }
Posted
Updated 21-Dec-22 11:20am
v2

That code won't compile:
C#
int str = textbox1.text;
is not valid C#: "text" is not a property of the TextBox class, "Text" is, and there is no implicit conversion in C# from string to any numeric type.
Additionally, the DoWork delegate is executed on a background worker thread, not the UI thread, so an attempt to access any Control in any way will cause a cross-threading exception - and that includes the TextBox.Text property.

Instead, you need convert the value using Tryparse:
C#
int val;
if (!int.TryParse(textbox1.text, out val))
   {
   ... report problem to user, it's not a number ...
   return;
   }
...
But that code needs to be executed on the GUI thread, not the background worker.

Handle the TextBox.TextChanged method and update a class-level variable which you can check in your worker thread: but do note that you will need to lock variables to prevent problems with multiple thread accesses, that 30 * Sleep(100) is only 3 seconds anyway, and that Hide doesn't close a form - it just removes it from view.
 
Share this answer
 
C#
int str = textbox1.text;

The TextBox Class (System.Windows.Forms) | Microsoft Learn[^] does not have a property named text. If you meant to specify Text, then that is still wrong as the Text property returns a string not an int.
 
Share this answer
 
A TextBox does not have a text property. The property is called Text - remember, C# is case-sensitive.

The Text property returns a string. You cannot assign that value to an int variable.

(I'm starting to suspect you're trying to convert some code from VB.NET here.)

The parameter to the ReportProgress method[^] is documented as "the percentage, from 0 to 100, of the background operation that is complete". However, in reality, there is no validation on the value you pass in. But if you use it to update a progress bar, it should be between the Minimum and Maximum values for that progress bar.

Fixing those errors gives you:
C#
private void Form_Load(object sender, EventArgs e)
{
    if (!int.TryParse(textbox1.Text, out int maximum)) maximum = 100;
    
    progressBar1.Minimum = 0;
    progressBar1.Maximum = maximum;
    
    backgroundWorker1.WorkerReportsProgress = true;
    backgroundWorker1.RunWorkerAsync(maximum);
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    int maximum = (int)e.Argument;
    for (int i = 0; i <= maximum; i++)
    {
        backgroundWorker1.ReportProgress(i);
        System.Threading.Thread.Sleep(100);
    }
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    updatepicbox();

    progressBar1.Value = e.ProgressPercentage;
    
    double percentComplete = (double)e.ProgressPercentage / progressBar1.Maximum;
    label1.Text = percentComplete.ToString("P0");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900