求winform 加载窗体时弹出另一个窗体并显示进度条的源码
发布网友
发布时间:2022-04-22 13:59
我来回答
共4个回答
热心网友
时间:2023-11-07 17:52
//frmA: 源窗体
//------------------------------------------
//引用
using System.Threading;
public frmA()
{
InitializeComponent();
worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
}
/// <summary>
/// 异步 开始事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
//需要执行的代码
}
/// <summary>
/// 事件: 异步执行完成后
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("执行完成。", "run", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//点击一个按钮后开始执行
private void btnA_Click(object sender, EventArgs e)
{
//代码
......
//异步执行开始
worker.RunWorkerAsync(tableName);
//显示进度窗体
frmB frm = new frmB(this.worker);
frm.StartPosition = FormStartPosition.CenterScreen;
frm.ShowDialog(this);
}
frmB:
//里面放了一个loading.gif图片显示进度
------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace test
{
public partial class frmB: Form
{
public frmB(BackgroundWorker worker)
{
InitializeComponent();
worker.ProgressChanged +=new ProgressChangedEventHandler(worker_ProgressChanged);
worker.RunWorkerCompleted +=new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.Close();
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//lblStatus.Text = "";
}
//工作完成后执行的事件
public void OnProcessCompleted(object sender, EventArgs e)
{
this.Close();
}
}
另外,站长团上有产品团购,便宜有保证
热心网友
时间:2023-11-07 17:52
大致做法:
private void ShowWaitting()
{
//frmWaitting相当于你题目中说的B窗口
frmWaitting tt = new frmWaitting();//这个是显示滚动条的窗口,里面做好了Timer和滚动条,
tt.ShowDialog();
tt.Dispose();
}
private void RefreshUI()//这是刷新界面,即你题目中的点击A窗口的按钮
{
Thread tGetData = new Thread(new ThreadStart(ShowWaitting));
tGetData.Name = "Waitting";
tGetData.Start();
//这里就是你要做的数据操作
//你的操作
//数据操作完毕
tGetData.Abort();
}
热心网友
时间:2023-11-07 17:53
进度条可以多次循环还是只能循环一次呀?多次循环就好办了。之间在B里添加属性(滚动条的value),用时间控件递加,A运行完后,再在A里面把B关了。
热心网友
时间:2023-11-07 17:54
多线程 + New Form Show + progressbar,这几种技术的组合,现成代码是没有的,你需要在网上分别找些例子阅读。