Winform打开时更改标签文本 [英] change label text while Winform is open

查看:205
本文介绍了Winform打开时更改标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在进度条中显示标签后更改标签的文本,
i希望显示正在上传的文件数量以及到目前为止的数量。



我已经创建了一个ProgressBar的winform,我的(初学者)计划是这样做的:

  public StatusUpload(String saved)
{
InitializeComponent();
timer1.Start();
timer1.Enabled = true;
AmountSaved.Text =保存;
}

但是当我尝试从另一个类中更改
i时,只在开始时定义它


  StatusUpload Progressbar = new StatusUpload(Total Saved:0 /+ selection.Count); 

不能再改变,我该怎么办?
(我想在循环后期更改它,所以我可以写1/2,然后在最后的循环2/2之后)

解决方案

如果你在后台做了很大的工作量。我会建议使用后台工作者: http://msdn.microsoft .com / de-de / library / system.componentmodel.backgroundworker.aspx



当您要上传数据时,您将生成Backgroundworker。然后你订阅Events:

 公共事件DoWorkEventHandler DoWork 

将您的上传代码放在这里。在你完成1/2文件之后,你可以调用 ReportProgress(1);

  public Event ProgressChangedEventHandler ProgressChanged 


$ b 如果调用 ReportProgress ; 。然后你更新你的进度栏:

  this.yourProgressBar.Value = e.ProgressPercentage; 

}

 公共事件RunWorkerCompletedEventHandler RunWorkerCompleted 

是您完成工作后抛出的事件。 / p>

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)

解决方案

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.

这篇关于Winform打开时更改标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆