首页 / C# / c# Winform 加载窗体
c# Winform 加载窗体
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# Winform 加载窗体,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4574字,纯文字阅读大概需要7分钟。
内容图文
![c# Winform 加载窗体](/upload/InfoBanner/zyjiaocheng/739/0d98f86ad8cc40fb921205546eefe6c6.jpg)
先来一个加载窗体代码
![c# Winform 加载窗体 - 文章图片](/upload/getfiles/0001/2021/5/4/20210504012934335.jpg)
![c# Winform 加载窗体 - 文章图片](/upload/getfiles/0001/2021/5/4/20210504012934449.jpg)
1 public partial class FrmLoading : Form 2 { 3 public BackgroundWorker updateDBWorker=new BackgroundWorker(); 4 5 public Action BackgroundWorkAction 6 { 7 get; 8 set; 9 } 10 11 public KeyValuePair<int, string> CurrentMsg 12 { 13 set 14 { 15 this.updateDBWorker.ReportProgress(value.Key, value.Value); 16 } 17 } 18 19 public FrmLoading() 20 { 21 InitializeComponent(); 22 this.updateDBWorker.WorkerReportsProgress = true; 23 this.updateDBWorker.WorkerSupportsCancellation = true; 24 this.updateDBWorker.DoWork += new DoWorkEventHandler(this.backgroundWorker1_DoWork); 25 this.updateDBWorker.ProgressChanged += new ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged); 26 this.updateDBWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted); 27 lblVer.Text = Properties.Resources.SystemVer; 28 } 29 30 31 public void ShowLog(string strLog, int intValue) 32 { 33 if (this.lblLog.InvokeRequired) 34 { 35 this.lblLog.BeginInvoke(new MethodInvoker(delegate() { ShowLog(strLog, intValue); })); 36 } 37 else 38 { 39 lblLog.Text = strLog; 40 this.progressBar1.Value = intValue; 41 } 42 } 43 44 private void FrmLoading_Load(object sender, EventArgs e) 45 { 46 this.updateDBWorker.RunWorkerAsync(); 47 } 48 49 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 50 { 51 if (this.BackgroundWorkAction != null) 52 { 53 this.BackgroundWorkAction(); 54 } 55 Thread.Sleep(100); 56 if (base.InvokeRequired) 57 { 58 base.BeginInvoke(new MethodInvoker(delegate 59 { 60 base.Close(); 61 })); 62 } 63 else 64 { 65 base.Close(); 66 } 67 } 68 69 private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) 70 { 71 ShowLog((e.UserState == null) ? "" : e.UserState.ToString(), e.ProgressPercentage); 72 } 73 74 private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 75 { 76 } 77 }View Code
界面就一个进度条,一个label,没其他东西
看调用的地方,Program文件里面
![c# Winform 加载窗体 - 文章图片](/upload/getfiles/0001/2021/5/4/20210504012934335.jpg)
![c# Winform 加载窗体 - 文章图片](/upload/getfiles/0001/2021/5/4/20210504012934449.jpg)
1 FrmLoading frmLoading = new FrmLoading(); 2 frmLoading.BackgroundWorkAction = delegate 3 { 4 try 5 { 6 //设置连接字符串 7 frmLoading.CurrentMsg = new KeyValuePair<int, string>(1, "正在初始化数据配置..."); 8 9 frmLoading.CurrentMsg = new KeyValuePair<int, string>(5, "正在初始化日志配置..."); 10 System.Threading.Thread.Sleep(200); 11 12 frmLoading.CurrentMsg = new KeyValuePair<int, string>(10, "正在升级本地数据..."); 13 14 frmLoading.CurrentMsg = new KeyValuePair<int, string>(50, "正在初始化本地参数..."); 15 16 frmLoading.CurrentMsg = new KeyValuePair<int, string>(85, "正在初始化热键信息..."); 17 18 frmLoading.CurrentMsg = new KeyValuePair<int, string>(90, "正在初始化硬件设备..."); 19 20 frmLoading.CurrentMsg = new KeyValuePair<int, string>(100, "初始化完成..."); 21 22 } 23 catch (Exception ex) 24 { 25 Application.Exit(); 26 Process.GetCurrentProcess().Kill(); 27 } 28 }; 29 frmLoading.ShowDialog(); 30 31 Application.Run(new form1());View Code
好了 就这样了,没什么技术含量,就不贴图了,拿去用吧
内容总结
以上是互联网集市为您收集整理的c# Winform 加载窗体全部内容,希望文章能够帮你解决c# Winform 加载窗体所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。