premise
The bank has 7, 8 years, and always wanted to do a nice custom control points, so there is this series of articles.
GitHub:https://github.com/kwwwvagaa/NetWinformControl
Cloud code: https://gitee.com/kwwwvagaa/net_winform_custom_control.git
If you feel that writing was okay, please support the point of a star about it
Come and explore the exchange: Penguins 568 015 492
Trouble blog below the point of a [Recommended], thank you
nuGet
Install-Package HZH_Controls
table of Contents
https://www.cnblogs.com/bfyx/p/11364884.html
Usefulness and effectiveness
Ready to work
This form is used in the base class (XVII) c # Winform custom control - form the base class , you do not know if you can first look at the venue
Start
Add a form FrmLoading inherited FrmBase
Something small, look at all the code
1 // *********************************************************************** 2 // Assembly : HZH_Controls 3 // Created : 2019-09-26 4 // 5 // *********************************************************************** 6 // <copyright file="FrmLoading.cs"> 7 // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:[email protected] 8 // </copyright> 9 // 10 // Blog: https://www.cnblogs.com/bfyx 11 // GitHub:https://github.com/kwwwvagaa/NetWinformControl 12 // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git 13 // 14 // If you use this code, please keep this note. 15 // *********************************************************************** 16 using System; 17 using System.Collections.Generic; 18 using System.ComponentModel; 19 using System.Data; 20 using System.Drawing; 21 using System.Linq; 22 using System.Text; 23 using System.Threading; 24 using System.Windows.Forms; 25 26 namespace HZH_Controls.Forms 27 { 28 /// <summary> 29 /// Class FrmLoading. 30 /// Implements the <see cref="HZH_Controls.Forms.FrmBase" /> 31 /// </summary> 32 /// <seealso cref="HZH_Controls.Forms.FrmBase" /> 33 public partial class FrmLoading : FrmBase 34 { 35 /// <summary> 36 /// The update database worker 37 /// </summary> 38 BackgroundWorker updateDBWorker = new BackgroundWorker(); 39 /// <summary> 40 /// 获取或设置加载任务 41 /// </summary> 42 /// <value>The background work action.</value> 43 public Action BackgroundWorkAction 44 { 45 get; 46 set; 47 } 48 /// <summary> 49 /// set the task name and the current execution progress, key: task progress, values 0-100 value: The current task name 50 /// </ Summary> 51 is /// <value> . The MSG of The Current </ value> 52 is public KeyValuePair < int , String > CurrentMsg 53 is { 54 is SET 55 { 56 is the this .updateDBWorker.ReportProgress (value.Key, value.Value); 57 is } 58 } 59 /// <Summary> 60 /// Initializes A new new instance of the<see cref="FrmLoading"/> class. 61 /// </summary> 62 public FrmLoading() 63 { 64 InitializeComponent(); 65 this.updateDBWorker.WorkerReportsProgress = true; 66 this.updateDBWorker.WorkerSupportsCancellation = true; 67 this.updateDBWorker.DoWork += new DoWorkEventHandler(this.backgroundWorker1_DoWork); 68 this.updateDBWorker.ProgressChanged += new ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged); 69 } 70 /// <Summary> 71 is /// setup progress, this function can be rewritten binding information processing interface 72 /// </ Summary> 73 is /// <param name = "strText" > schedule task name </ param> 74 /// <param name = "intValue"> progress value </ param> 75 protected Virtual void BindingProcessMsg ( String strText, int intValue) 76 { 77 78 } 79 80 /// <Summary > 81 /// Sets the message. 82 /// </summary> 83 /// <param name="strText">The string text.</param> 84 /// <param name="intValue">The int value.</param> 85 private void SetMessage(string strText, int intValue) 86 { 87 if (this.InvokeRequired) 88 { 89 this.BeginInvoke(new MethodInvoker(delegate() { SetMessage(strText, intValue); })); 90 } 91 else 92 { 93 BindingProcessMsg(strText, intValue); 94 } 95 } 96 97 /// <summary> 98 /// Handles the Load event of the FrmLoading control. 99 /// </summary> 100 /// <param name="sender">The source of the event.</param> 101 /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> 102 private void FrmLoading_Load(object sender, EventArgs e) 103 { 104 if (ControlHelper.IsDesignMode()) 105 return; 106 this.updateDBWorker.RunWorkerAsync(); 107 } 108 109 /// <summary> 110 /// Handles the DoWork event of the backgroundWorker1 control. 111 /// </summary> 112 /// <param name="sender">The source of the event.</param> 113 /// <param name="e">The <see cref="DoWorkEventArgs"/> instance containing the event data.</param> 114 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 115 { 116 if (this.BackgroundWorkAction != null) 117 { 118 this.BackgroundWorkAction(); 119 } 120 Thread.Sleep(100); 121 if (base.InvokeRequired) 122 { 123 base.BeginInvoke(new MethodInvoker(delegate 124 { 125 base.Close(); 126 })); 127 } 128 else 129 { 130 base.Close(); 131 } 132 } 133 134 /// <summary> 135 /// Handles the ProgressChanged event of the backgroundWorker1 control. 136 /// </summary> 137 /// <param name="sender">The source of the event.</param> 138 /// <param name="e">The <see cref="ProgressChangedEventArgs"/> instance containing the event data.</param> 139 private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) 140 { 141 SetMessage((e.UserState == null) ? "" : e.UserState.ToString(), e.ProgressPercentage); 142 } 143 } 144 }
Description:
BackgroundWorkAction: Load Resources Task function
CurrentMsg: current progress message display, key: task progress, values 0-100 value: the name of the current task
BindingProcessMsg: Binding data to the interface, subclasses need to override this function to achieve the binding data to a display screen
Example:
Add a form FrmTestLoading inherited FrmLoading
Add a text label1 text displays progress information
Add a progress bar shows the progress of the value ucProcessLineExt1
Re BindingProcessMsg binding information
1 protected override void BindingProcessMsg(string strText, int intValue) 2 { 3 label1.Text = strText; 4 this.ucProcessLineExt1.Value = intValue; 5 }
transfer
1 FrmTestLoading frmLoading = new FrmTestLoading(); 2 frmLoading.BackgroundWorkAction = delegate() 3 { 4 try 5 { 6 frmLoading.CurrentMsg = new KeyValuePair<int, string>(1, "正在初始化配置..."); 7 Thread.Sleep(1000); 8 frmLoading.CurrentMsg = newKeyValuePair < int , String > ( 10 , " Loading of a resource ... " ); . 9 the Thread.Sleep ( 1000 ); 10 frmLoading.CurrentMsg = new new KeyValuePair < int , String > ( 20 is , " Loading second resources ... " ); . 11 the Thread.Sleep ( 1000 ); 12 is frmLoading.CurrentMsg = new new KeyValuePair < int , String> ( 30 , " Loading third resource ... " ); 13 is the Thread.Sleep ( 1000 ); 14 frmLoading.CurrentMsg = new new KeyValuePair < int , String > ( 40 , " Loading ... fourth resource " ); 15 the Thread.Sleep ( 1000 ); 16 frmLoading.CurrentMsg = new new KeyValuePair < int , String > ( 50 , "Loading fifth resource ... " ); . 17 the Thread.Sleep ( 1000 ); 18 is frmLoading.CurrentMsg = new new KeyValuePair < int , String > ( 60 , " Loading sixth resource ... " ); . 19 the Thread .sleep ( 1000 ); 20 is frmLoading.CurrentMsg = new new KeyValuePair < int , String > ( 70 , " loading ... seventh resource " ); 21 The Thread.Sleep ( 1000 ); 22 is frmLoading.CurrentMsg = new new KeyValuePair < int , String > ( 80 , " Loading eighth resource ... " ); 23 is the Thread.Sleep ( 1000 ); 24 frmLoading.CurrentMsg = new new KeyValuePair < int , String > ( 90 , " loading ninth resources ... " ); 25 the Thread.Sleep ( 1000); 26 is frmLoading.CurrentMsg = new new KeyValuePair < int , String > ( 1000 , " Data loaded ... " ); 27 the Thread.Sleep ( 1000 ); 28 } 29 the catch (Exception EX) 30 { 31 is MessageBox.Show ( " error loading resources " ); 32 } 33 }; 34 frmLoading.ShowDialog();
Last words
If you like it, please https://gitee.com/kwwwvagaa/net_winform_custom_control point stars right