我想在C#windows Forms中显示POPUP进度条 [英] I want to show POPUP progressbar in C# windows Forms

查看:78
本文介绍了我想在C#windows Forms中显示POPUP进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我创建了一个包含组合框和按钮的C#windows应用程序。



当用户从组合框中选择一个值时,会在数据库上触发查询



这需要花费一段时间才能执行。



我在表单上显示一个进度条,通知用户,但是为了避免用户点击按钮并选择其他组合框,请按顺序



直到



操作结束我想显示弹出进度条。



我知道这可以做到使用showdialog,但如何关闭showdialog表格



该函数完成执行。



另外如果你有任何其他选择,请建议。



谢谢,



祝你有个美好的一天,

=====================

Kishor Kadam

联系方式:[删除]

============== ========

Hi,

I have created a C# windows application which contains comboboxes and buttons.

When a user selects a value from the combobox a query is fired on the database

which takes a noticable time to execute.

I am displaying a progressbar on the form to notify this to the user but in order

to avoid user from clicking the buttons and selecting other comboboxes till the

operation finishes I want to show a popup progressbar.

I know this can be done using showdialog but how to close the showdialog form when

the function finishes its execution.

Also if you have any other alternative,please suggest.

Thank you,

Have a good day ahead,
=====================
Kishor Kadam
Contact:[Deleted]
======================

推荐答案

当然您正在更新进度条的值。一旦进度条为> = 100%,只需调用myDialog.Close();
Surely you are updating the value of the progress bar. Once the progress bar is >= 100% just call myDialog.Close();


这很容易使用'BackGroundWorker组件和'ProgressBar控件。



'BackGroundWorker为您提供了一种异步运行操作的简单方法。



因为你想冻结用户界面,你需要使用'ShowDialog,所以你需要一个表格来放置你的ProgressBar。在该表单中,我们需要为外部客户提供更新ProgressBar的方法:
This is really easy to using a 'BackGroundWorker Component, and a 'ProgressBar Control.

The 'BackGroundWorker gives you an easy way to run an operation asynchronously.

Since you want to "freeze" the UI, you need to use 'ShowDialog, so you will need a Form to put your ProgressBar on. In that Form we need to provide a way for an external "customer" to update the ProgressBar:
// expose a method to set the ProgressBar #value
public void SetProgressBarValue(int pbValue)
{
    progressBar1.Value = pbValue;
}

BackGroundWorker公开了你可以处理的三个主要事件:



1. DoWork:这是你开始的地方查询,这是你必须弄清楚如何有意义地更新ProgressBar的地方。



2. ProgressChanged:这个事件奇怪地通过使用ReportProgress方法调用BackGroundWorker传递一个整数值,在这个EventHandler中你将更新ProgressBar。



3. RunWorkerCompleted:当BackGroundWorker完成时为你触发,这就是你将关闭显示DialogBox的表格的地方。



假设你的带有ProgressBar的表格被命名为'ProgressBarForm,并且在你的主表单的代码中你有创建了一个分配给名为'frmPgBar的变量的实例,你的BackGroundWorker组件(从ToolBox拖放到你的主窗体上)被命名为'bgwQuery



开始BackGroundWorke r:



1.在您的EventHandler中,现在触发查询的用户界面元素:

The BackGroundWorker exposes three main Events that you can handle:

1. DoWork: this is where you will start the Query, and this is where you must figure out how to meaningfully update the ProgressBar.

2. ProgressChanged: this Event which is, strangely, called by using the ReportProgress method of the BackGroundWorker is passed an integer value, and it is in this EventHandler you'll update the ProgressBar.

3. RunWorkerCompleted: triggered for you when the BackGroundWorker is done, and this is where you will close the Form that displays the DialogBox.

Assuming your Form with the ProgressBar is named 'ProgressBarForm, and in your main Form's code you have created an instance of it assigned to a variable named 'frmPgBar, and your BackGroundWorker Component (drag-dropped onto your main form from the ToolBox) is named 'bgwQuery

Starting the BackGroundWorker:

1. in your EventHandler for the user-interface element that now triggers the Query:

bgwQuery.RunWorkerAsync();
frmPg.ShowDialog();

现在你的'DoWork EventHandler中的代码将被执行,你面临的挑战是如何更新ProgressBar以及一些有意义的值,以便当您在'DoWork代码中调用'ReportProgress(#integer)时:

Now your code in your 'DoWork EventHandler is going to be executed, and you face the challenge of figuring out how to update the ProgressBar with some meaningful value so that when you call 'ReportProgress(#integer) in your 'DoWork code:

private void bgwQuery_DoWork(object sender, DoWorkEventArgs e)
{    
     while (? some condition)
     {
        // do stuff

        // generate an integer value in the range 0~100
        // for the update
        bgwQuery.ReportProgress(#integer);
     }
}

在ProgressChanged EventHandler中:

In the ProgressChanged EventHandler:

private void bgwQuery_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // update the ProgressBar in frmPgBar
    frmPgBar.SetProgressBarValue(e.ProgressPercentage);
}

最后,在RunWorkerCompleted处理程序中,您可以使用ProgressBar关闭Form:

Finally, in your RunWorkerCompleted handler, you can close the Form with the ProgressBar:

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    frmPgBar.Close();
}

注意BackGroundWorker的其他高级用法,包括将参数传递到'DoWork EventHandler,使用分配到DoWork的DoWorkEventArgs的'Result字段以将数据传输回主UI线程等。 ,请参阅MSDN文档。

Note for other advanced uses of BackGroundWorker including passing parameters into the 'DoWork EventHandler, using assignment to the 'Result field of DoWork's DoWorkEventArgs to transfer data back to the main UI thread, etc., see MSDN docs.


这篇关于我想在C#windows Forms中显示POPUP进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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