C# WinForms 中的进度条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/756932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Progress Bar in WinForms
提问by JayJay
i need to insert a progress bar between 2 forms ,i have a Main Form when i click a button to open a window i need wait before to load the last one window ('cos there are many picture to download on the last one Form) and i decide to use a Progress Bar to show the time remaining to open the Window requested. Now i don't know how implement in code this feature (it's the first time i use a Progress Bar) . Do you have any advice to help me how work out this feature? Thanks for your attention.
我需要在 2 个表单之间插入一个进度条,当我单击一个按钮打开一个窗口时,我有一个主表单,我需要等待加载最后一个窗口(因为最后一个表单上有很多图片要下载)我决定使用进度条来显示打开请求的窗口的剩余时间。现在我不知道如何在代码中实现这个功能(这是我第一次使用进度条)。你有什么建议可以帮助我如何解决这个功能吗?感谢您的关注。
P.S .Sorry from my bad english
PS.对不起,我的英语不好
回答by northpole
Here is a simple example....
这是一个简单的例子......
There are only three members of the ProgressBar class you should know about. The Maximum, the Minimum, and the Value properties.
您应该只知道 ProgressBar 类的三个成员。最大值、最小值和值属性。
You create a progress bar control using ProgressBar constructor.
您可以使用 ProgressBar 构造函数创建一个进度条控件。
this.progressBar1 = new System.WinForms.ProgressBar();
After creating instance of a progress bar you set the range of the progress bar by using Minimum and Maximum properties of the ProgressBar.
创建进度条的实例后,您可以使用 ProgressBar 的 Minimum 和 Maximum 属性设置进度条的范围。
progressBar1.Maximum = 200;
progressBar1.Manimum=0;
The Step property is used to set number of steps in a progress bar.
Step 属性用于设置进度条中的步数。
progressBar1.Step=20;
The Value property is used to set the current value of the status bar.
Value 属性用于设置状态栏的当前值。
protected void displayProgress (object sender, System.EventArgs e)
{
if (progressBar1.Value >= 200 )
{
progressBar1.Value = 0;
return;
}
progressBar1.Value += 20;
}
回答by Dillie-O
I'd recommend implementing the BackgroundWorkerobject in your Winforms application.
我建议在您的 Winforms 应用程序中实现BackgroundWorker对象。
This will provide an easy multithreaded approach to doing something processing intensive without locking up your application. You can use this in conjunction with the ProgressBar, or you can even setup your own process indicator.
这将提供一种简单的多线程方法,可以在不锁定应用程序的情况下进行密集处理。您可以将它与 ProgressBar 结合使用,或者您甚至可以设置自己的流程指示器。
回答by Mez
If you know how many pictures the new window will be downloading it is rather easy to implement the progress bar.
如果您知道新窗口将下载多少张图片,那么实现进度条就很容易了。
Just drop a progressbar somewhere on your form in the form-designer of Visual studio. Now you can use the progressbar in your code like the following:
只需在 Visual Studio 的表单设计器中的表单某处放置一个进度条。现在您可以在代码中使用进度条,如下所示:
MyProgressBar.Minimum = 0;
MyProgressBar.Maximum = TotalPicturesToDownload;
for (int i = 1; i<= TotalPicturesToDownload; i++)
{
//Do whatever necessary to download picture nr i
//....
//Update the progressBar
MyProgressBar.Increment(1);
}
Like already mentioned it is advisable to not do this in the UI thread but on a separate thread (use eg a BackGroundWorker) otherwise your form could be freezing up and making your progressbar not to update.
就像已经提到的那样,建议不要在 UI 线程中执行此操作,而是在单独的线程上执行此操作(例如使用 BackGroundWorker),否则您的表单可能会冻结并使您的进度条无法更新。