change label text while Winform is open [closed]

我们两清 提交于 2019-12-12 01:12:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!