进度条和数据加载

发布于 2024-12-15 06:33:56 字数 887 浏览 1 评论 0原文

我有 C# 中的 Windows 窗体,具有带有大号的 Datagridview。来自数据库的记录和一些组合框、文本框和按钮。

因此,我使用了另一种具有进度条和后台工作程序的表单,以便主表单的数据加载不会刺激最终用户。

 public partial class FirstForm : Form
{
    MainForm mf;
    public FirstForm()
    {
        InitializeComponent();
      backgroundWorker1.RunWorkerAsync();
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        mf = new MainForm(); //inside constructor,code of data loading in gridview
        mf.Update();
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        if (p1.Value < p1.Maximum)         //p1 name for progressbar
            p1.Value++;
        else
        {
            timer1.Enabled = false;
            this.Hide();
            mf.Show();
        }
    }

}

但是当主窗体显示时,它是空白的,2/3 秒后 datagridview 和其他控件出现。

如何解决这个问题..?
或提出其他想法来解决这个问题。

i have Windows Form in C# having Datagridview with large no. of records from database and some comboboxes,textbox and buttons.

so,i used another form having progressbar and backgroundworker so that data loading of mainform does not iritate enduser.

 public partial class FirstForm : Form
{
    MainForm mf;
    public FirstForm()
    {
        InitializeComponent();
      backgroundWorker1.RunWorkerAsync();
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        mf = new MainForm(); //inside constructor,code of data loading in gridview
        mf.Update();
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        if (p1.Value < p1.Maximum)         //p1 name for progressbar
            p1.Value++;
        else
        {
            timer1.Enabled = false;
            this.Hide();
            mf.Show();
        }
    }

}

but when main form is displayed,it is blank and after 2/3 seconds datagridview and other controls appear.

how to solve this..?
or suggest other ideas to solve this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ记忆︶ㄣ 2024-12-22 06:33:56

删除 Firstform 中的代码并将我的代码写入
programs.cs

static void Main()
{
    Application.EnableVisualStyles();
    Application.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

    Application.SetCompatibleTextRenderingDefault(false);

        System.ComponentModel.BackgroundWorker bw = new System.ComponentModel.BackgroundWorker();
        bw.DoWork += new System.ComponentModel.DoWorkEventHandler(bw_DoWork);
        bw.WorkerSupportsCancellation = true;
        MainForm = new MainForm(); // creating main form

bw.RunWorkerAsync();
frm.Initiate(); // 将此方法添加到第一个表单以加载和启动

        bw.CancelAsync(); // ending splashing
        Application.Run(frm);
}     

static void bw_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
    AFirstForm splashForm = new FirstForm();
    splashForm.TopMost = true;
    splashForm.Show();
    while (!(sender as System.ComponentModel.BackgroundWorker).CancellationPending)
    {
        splashForm.Refresh();
    }
    splashForm.Close();    
    e.Result = splashForm;
}

Remove your code in Firstform and write mine in
programs.cs

static void Main()
{
    Application.EnableVisualStyles();
    Application.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

    Application.SetCompatibleTextRenderingDefault(false);

        System.ComponentModel.BackgroundWorker bw = new System.ComponentModel.BackgroundWorker();
        bw.DoWork += new System.ComponentModel.DoWorkEventHandler(bw_DoWork);
        bw.WorkerSupportsCancellation = true;
        MainForm = new MainForm(); // creating main form

bw.RunWorkerAsync();
frm.Inittiate(); // Add this method to first form to loading and initiating

        bw.CancelAsync(); // ending splashing
        Application.Run(frm);
}     

static void bw_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
    AFirstForm splashForm = new FirstForm();
    splashForm.TopMost = true;
    splashForm.Show();
    while (!(sender as System.ComponentModel.BackgroundWorker).CancellationPending)
    {
        splashForm.Refresh();
    }
    splashForm.Close();    
    e.Result = splashForm;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文