1

我正在开发一个将 OLE 数据库加载到 DataGridView 中的应用程序。虽然数据库文件是本地存储的,但是应用程序加载数据库需要一些时间,所以我只得到一个“加载”游标。我想要更多:我想创建一个进度条以在数据库加载时显示并在数据库完全加载时隐藏。

我在谷歌上搜索,但没有找到我要找的东西。我能做些什么?

(我在 Visual Studio 中工作,所以请记住,数据集的整个代码都是由 IDE 自动编写的)

4

1 回答 1

2

您可能正在寻找与 ProgressBar 结合使用的 BackgroundWorker,并将其放在您的表单上并使用以下代码:

public partial class Form1 : Form
{
public Form1()
{
    InitializeComponent();
    Shown += new EventHandler(Form1_Shown);

    // To report progress from the background worker we need to set this property
    backgroundWorker1.WorkerReportsProgress = true;
    // This event will be raised on the worker thread when the worker starts
    backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
    // This event will be raised when we call ReportProgress
    backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
}
void Form1_Shown(object sender, EventArgs e)
{
    // Start the background worker
    backgroundWorker1.RunWorkerAsync();
}
// On worker thread so do our thing!
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    // Your background task goes here
    for (int i = 0; i <= 100; i++)
    {
        // Report progress to 'UI' thread
        backgroundWorker1.ReportProgress(i);
        // Simulate long task
        System.Threading.Thread.Sleep(100);
    }
}
// Back on the 'UI' thread so we can update the progress bar
void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // The progress percentage is a property of e
    progressBar1.Value = e.ProgressPercentage;
}

}

这只是一个如何使用它的例子。您将需要修改:

backgroundWorker1.ReportProgress(i);

因此,它实际上是在报告与 OLE 数据库发生的事情有关的进展。

于 2013-08-09T19:12:56.293 回答