C# WinForm窗体如何在窗体加载时显示进度条???

nlqtonglin2006 2009-06-29 10:33:57
在打开子窗体时 访问不不访问数据库窗体打开都很慢,如何在打开时显示正在加载 不让用户感到死机
...全文
4346 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
yiqinqian 2010-10-12
  • 打赏
  • 举报
回复
急需,代码提供·····
lenovore 2010-09-09
  • 打赏
  • 举报
回复
关注!!!
qqiuzaihui 2009-06-29
  • 打赏
  • 举报
回复
使用线程, 给你一个例子, 你研究一下。 把它的计算数列过程改成加载数据就行了。


//例: 计算Fibonacci数列,并实时滚动进度条
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.ComponentModel;

class FibonacciNumber : Form
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new FibonacciNumber());
}

private StatusStrip progressStatusStrip;
private ToolStripProgressBar toolStripProgressBar;
private NumericUpDown requestedCountControl;
private Button goButton;
private TextBox outputTextBox;
private BackgroundWorker backgroundWorker;
private ToolStripStatusLabel toolStripStatusLabel;
private int requestedCount;

public FibonacciNumber()
{
Text = "Fibonacci";

// Prepare the StatusStrip.
progressStatusStrip = new StatusStrip();
toolStripProgressBar = new ToolStripProgressBar();
toolStripProgressBar.Enabled = false;
toolStripStatusLabel = new ToolStripStatusLabel();
progressStatusStrip.Items.Add(toolStripProgressBar);
progressStatusStrip.Items.Add(toolStripStatusLabel);

FlowLayoutPanel flp = new FlowLayoutPanel();
flp.Dock = DockStyle.Top;

Label beforeLabel = new Label();
beforeLabel.Text = "Calculate the first ";
beforeLabel.AutoSize = true;
flp.Controls.Add(beforeLabel);
requestedCountControl = new NumericUpDown();
requestedCountControl.Maximum = 1000;
requestedCountControl.Minimum = 1;
requestedCountControl.Value = 100;
flp.Controls.Add(requestedCountControl);
Label afterLabel = new Label();
afterLabel.Text = "Numbers in the Fibonacci sequence.";
afterLabel.AutoSize = true;
flp.Controls.Add(afterLabel);

goButton = new Button();
goButton.Text = "&Go";
goButton.Click += new System.EventHandler(button1_Click);
flp.Controls.Add(goButton);

outputTextBox = new TextBox();
outputTextBox.Multiline = true;
outputTextBox.ReadOnly = true;
outputTextBox.ScrollBars = ScrollBars.Vertical;
outputTextBox.Dock = DockStyle.Fill;

Controls.Add(outputTextBox);
Controls.Add(progressStatusStrip);
Controls.Add(flp);

backgroundWorker = new BackgroundWorker();
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);

}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// This method will run on a thread other than the UI thread.
// Be sure not to manipulate any Windows Forms controls created
// on the UI thread from this method.
backgroundWorker.ReportProgress(0, "Working...");
Decimal lastlast = 0;
Decimal last = 1;
Decimal current;
if (requestedCount >= 1)
{ AppendNumber(0); }
if (requestedCount >= 2)
{ AppendNumber(1); }
for (int i = 2; i < requestedCount; ++i)
{
// Calculate the number.
checked { current = lastlast + last; }
// Introduce some delay to simulate a more complicated calculation.
System.Threading.Thread.Sleep(100);
AppendNumber(current);
backgroundWorker.ReportProgress((100 * i) / requestedCount, "Working...");
// Get ready for the next iteration.
lastlast = last;
last = current;
}


backgroundWorker.ReportProgress(100, "Complete!");
}

private delegate void AppendNumberDelegate(Decimal number);
private void AppendNumber(Decimal number)
{
if (outputTextBox.InvokeRequired)
{ outputTextBox.Invoke(new AppendNumberDelegate(AppendNumber), number); }
else
{ outputTextBox.AppendText(number.ToString("N0") + Environment.NewLine); }
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
toolStripProgressBar.Value = e.ProgressPercentage;
toolStripStatusLabel.Text = e.UserState as String;
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error is OverflowException)
{ outputTextBox.AppendText(Environment.NewLine + "**OVERFLOW ERROR, number is too large to be represented by the decimal data type**"); }
toolStripProgressBar.Enabled = false;
requestedCountControl.Enabled = true;
goButton.Enabled = true;

}

private void button1_Click(object sender, EventArgs e)
{
goButton.Enabled = false;
toolStripProgressBar.Enabled = true;
requestedCount = (int)requestedCountControl.Value;
requestedCountControl.Enabled = false;
outputTextBox.Clear();
backgroundWorker.RunWorkerAsync();
}
}
sl2161 2009-06-29
  • 打赏
  • 举报
回复
多线程,使用BackgroundWorker组件的ProgressChanged事件

推荐文章一篇
nlqtonglin2006 2009-06-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yangxuebao123 的回复:]
做一个假象,图片
[/Quote]

具体如何实现啊??
yangxuebao123 2009-06-29
  • 打赏
  • 举报
回复
用个线程 去控制。
yangxuebao123 2009-06-29
  • 打赏
  • 举报
回复
做一个假象,图片
deyter 2009-06-29
  • 打赏
  • 举报
回复
关注,我也要做个这个东西
nlqtonglin2006 2009-06-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 IHandler 的回复:]
使用Progressbar(使用Marquee就先行了),异步加载数据,
[/Quote]
能详细点吗? 小弟新手!!
nlqtonglin2006 2009-06-29
  • 打赏
  • 举报
回复
给全详细点的说法吧,小弟没做过这个东西!!
IHandler 2009-06-29
  • 打赏
  • 举报
回复
使用Progressbar(使用Marquee就先行了),异步加载数据,
lzc2125 2009-06-29
  • 打赏
  • 举报
回复
自己做一个进度条画面,实装一下
haonanxxx 2009-06-29
  • 打赏
  • 举报
回复
ajax,
或者用微软的ProgessBar
Deathsign 2009-06-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 haonanxxx 的回复:]
ajax,
或者用微软的ProgessBar
[/Quote]

亮了
codelabs 2009-06-29
  • 打赏
  • 举报
回复
.net有自带的这中加载窗体前的启动功能,叫做创建启动屏幕(我是这么称呼它的)。
ApplicationContext基本上需要的东西覆盖了。
fanglong168 2009-06-29
  • 打赏
  • 举报
回复
mark 一下 以后学习
编程有钱人了 2009-06-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 haonanxxx 的回复:]
ajax,
或者用微软的ProgessBar
[/Quote]

c# winform下页可以用Ajax?

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧