问题
I am trying to Change the text of a Label after it is shown in a Progressbar, i want to Show the number of files that are being uploaded, and the number that has been so far.
i have created a ProgressBar winform, and my (beginner) plan was to do it like this:
public StatusUpload(String saved)
{
InitializeComponent();
timer1.Start();
timer1.Enabled = true;
AmountSaved.Text = saved;
}
but when i try to Change it from another class, i can only define it in the beginning
StatusUpload Progressbar = new StatusUpload("Total Saved: 0/" + selection.Count);
and can't Change it afterwards anymore, what should i do? (i want to Change it later during the Loop so i can write 1/2, and then after the final Loop 2/2)
回答1:
If you are doing a big workload in the background. I would advise using a Background Worker :http://msdn.microsoft.com/de-de/library/system.componentmodel.backgroundworker.aspx
You generate the Backgroundworker when you want to upload your data. Then you subscribe to the Events:
public event DoWorkEventHandler DoWork
Put your uploading code here. After i.e. you finished 1/2 files you call ReportProgress(1);
public event ProgressChangedEventHandler ProgressChanged
This is thrown if you call ReportProgress();
. Then you update your Progressbar with:
this.yourProgressBar.Value = e.ProgressPercentage;
}
public event RunWorkerCompletedEventHandler RunWorkerCompleted
is the event which is thrown after you finished your work.
来源:https://stackoverflow.com/questions/16750498/change-label-text-while-winform-is-open