在我的应用程序上显示进度条

时间:2012-12-21 18:28:07

标签: c# vb.net

我是Windows编程的新手,我有一个Windows应用程序,点击按钮,它调用Web服务和  从Web服务获取数据,然后将值插入数据库。我需要展示所有这一切的progeress酒吧。下面是我的代码

private void btnService_Click(object sender, EventArgs e)
{
    //call to the web service
        //get the data
        //insert the returned data from web service to the database.   
}

我将进度条控件放在我的页面上,但在我看来,我必须为进度条分配一些数字,以便显示状态。

1 个答案:

答案 0 :(得分:1)

如果您正在使用阻止进度条,则可以像这样更新其状态

int totalSteps = 10;
for (int i= 1; i<= totalSteps; i++)
{
    //  One chunk of your code

    int progress = i * 100 / totalSteps;
    blocksProgressBar.Value = progress;
}
blocksProgressBar.Value = 0;

如果您使用后台工作程序,则栏可能会像这样更新

private void btnService_Click(object sender, EventArgs e)
{
    backgroundWorker.RunWorkerAsync();
}

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    int totalSteps = 10;

    for (int i = 1; i <= totalSteps; i++)
    {
        //  One chunk of your code

        int progress = i * 100 / totalSteps;
        backgroundWorker.ReportProgress(progress);
    }
}

private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    blocksProgressBar.Value = e.ProgressPercentage;
}

private void backgroundWorker_Completed(object sender, RunWorkerCompletedEventArgs e)
{
    blocksProgressBar.Value = 0;
}

Source