如何在Windows窗体C#中实现进度条? [英] How to implement a progress bar in windows forms C#?

查看:847
本文介绍了如何在Windows窗体C#中实现进度条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自己的解决方案,可以在Windows窗体应用程序中导入每月销售数据.当用户单击import按钮时,该程序实际上正在运行,但似乎没有响应.该过程耗时约5分钟.

I have my own solution to import monthly sales data in my windows form application. When a user click on import button, the program is actually running but it looks like it's not responding. The process takes a long time about 5 minutes.

因此,我想实现一个带有状态条标签的进度条,以显示为用户界面,并让用户知道该任务完成了多少.这也是我第一次在程序中使用进度条.因此,我通读了一些教程,展示了如何使用它.某些人将进度条与后台工作人员和计时器一起使用.

So, I'd like to implement a progress bar with status strip label to display as an user interface and let the users know how much the task is done. This is also my first time using a progress bar in my program. So, I read through some tutorials which show how to use it. Some people use progress bar with background worker and timer.

但是我不知道应该在哪里使用现有的解决方案.在后台工作人员DoWork()事件中?我不想通过滥用进度条来伪造它,例如设置progressBar.Maximum = 100,progressBar.Value = 0,并且只要计时器将值增加5即可.进度条必须报告实际进度,同时该程序正在运行.

But I don't understand where I should use the solution that I have. In background worker DoWork() event? I don't want to fake it by abusing the progress bar like setting the progressBar.Maximum = 100, progressBar.Value = 0 and as long as the timer is ticking increasing the value by 5. The progress bar must report the actual progress while the program is running.

以下是我现在用于导入数据的解决方案:

The following is the solution I am using now to import the data:

private void btnImport_Click(object sender, EventArgs e)
    {
        if (lsbxBrowsedFiles.Items.Count != 0)
        {
            ArrayList salesHeaderArr = new ArrayList();
            ArrayList salesDetailArr = new ArrayList();

            int i = 0;
            while (i < browsedXmlFileList.Count)
            {
                if (browsedXmlFileList[i].ToUpper().EndsWith("SALESHEADER.XML"))
                {
                    salesHeaderArr.Add(browsedXmlFileList[i]);
                }
                if (browsedXmlFileList[i].ToUpper().EndsWith("SALESDETAIL.XML"))
                {
                    salesDetailArr.Add(browsedXmlFileList[i]);
                }
                i++;
            }

            if (selectedFileIsNotInDestinationFolder(salesHeaderArr, salesDetailArr) == true)
            {
                i = 0;
                while (i < salesHeaderArr.Count)
                {
                    SalesHeader salesHeader = new SalesHeader();
                    string sourceFilePath = salesHeaderArr[i].ToString();
                    readXMLFiles(sourceFilePath, SALES_HEADER);
                    SalesHeader salesCheck = (SalesHeader)salesHeaderList[0];
                    string checkOutletCode = salesCheck.OutletCode;
                    DateTime checkBusDate = salesCheck.BusinessDate.Value;
                    if (SalesHeader.IsThisRowAlreadyImportedInSalesHeader(checkOutletCode, checkBusDate) == false)
                    {
                        salesHeader.ImportSalesHeader(salesHeaderList);
                        salesHeader.CreateImportDataLog(getDestinationFilePath(sourceFilePath),
                            DateTime.Now, salesHeaderList.Count, SALES_HEADER);
                    }
                    else
                    {
                        string errorDate = checkBusDate.ToString("dd MMMM, yyyy");
                        MessageBox.Show("Selected XML File with BusinessDate: " + errorDate + " has been already imported.",
                            "ABC Cafe Import Sales Wizard");
                        MessageBox.Show("Please select a file which has not been imported!",
                            "ABC Cafe Import Sales Wizard");
                        return;
                    }
                    MoveXMLFiletoDestinationFolder(sourceFilePath);
                    i++;
                }
                i = 0;
                while (i < salesDetailArr.Count)
                {
                    SalesDetail salesDetail = new SalesDetail();
                    string sourceFilePath = salesDetailArr[i].ToString();
                    readXMLFiles(sourceFilePath, SALES_DETAIL);
                    SalesDetail salesCheck = (SalesDetail)salesDetailList[0];
                    string checkOutletCode = salesCheck.OutletCode;
                    DateTime checkBusDate = salesCheck.BusinessDate.Value;
                    if (SalesDetail.IsThisRowAlreadyImportedInSalesDetail(checkOutletCode, checkBusDate) == false)
                    {
                        salesDetail.ImportSalesDetail(salesDetailList);
                        salesDetail.GenerateCarryForward(salesDetailList);
                        salesDetail.CalculateImportInventoryBalance(salesDetailList);
                        salesDetail.CreateImportDataLog(getDestinationFilePath(sourceFilePath), DateTime.Now, salesDetailList.Count, SALES_DETAIL);
                    }
                    else
                    {
                        string errorDate = checkBusDate.ToString("dd MMMM, yyyy");
                        MessageBox.Show("Selected XML File with BusinessDate: " + errorDate + " has been already imported.",
                            "ABC Cafe Import Sales Wizard");
                        MessageBox.Show("Please select a file which has not been imported!",
                            "ABC Cafe Import Sales Wizard");
                        return;
                    }
                    MoveXMLFiletoDestinationFolder(sourceFilePath);
                    i++;
                }
                MessageBox.Show("Import has been successfully completed!",
                "ABC Cafe Import Sales Wizard");
                clearListBoxItems();
                lblMessage.Visible = false;
            }
            //Abort the import operation here!
            else
            {
                MessageBox.Show("Please select a file which has not been imported!",
                "ABC Cafe Import Sales Wizard");
                clearListBoxItems();
                lblMessage.Visible = false;
            }
        }
        else
        {
            MessageBox.Show("Please select XML files to import!", 
                "ABC Cafe Import Sales Wizard");
        }
    }

任何帮助将不胜感激!

推荐答案

您的程序有很多循环,因为在每次循环迭代中都很难获得增量值和增量进度条值.您可以设置进度条最大为100,然后用100除以您说X的循环数.

Your program has many loops,since it will be difficult to get the increment value and increment the progress bar value in each loop iteration.You could set the progress bar maximum to 100,Then divide 100 by the number of loops you have say X.

所以诀窍是在每个循环完成时用此值填充进度栏

So the trick would be to fill the progress bar by this value when each loops completes

是的,您应该将此代码放在backgroundworker的DoWork()中,否则它将冻结表单.而且不需要计时器.

and yes you should put this code in backgroundworker's DoWork() otherwise it will freeze the form.Also there is no need for a timer.

这篇关于如何在Windows窗体C#中实现进度条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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