(七)c#Winform自定義控件-進度條

官網

http://www.hzhcontrols.comhtml

前提

入行已經7,8年了,一直想作一套漂亮點的自定義控件,因而就有了本系列文章。git

GitHub:https://github.com/kwwwvagaa/NetWinformControlgithub

碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_control.gitc#

若是以爲寫的還行,請點個 star 支持一下吧編輯器

歡迎前來交流探討: 企鵝羣568015492 企鵝羣568015492ide

目錄

http://www.javashuo.com/article/p-hacmmtru-mw.htmlpost

準備工做

該控件將繼承基類控件UCControlBase,若是你還對UCControlBase不瞭解,請移步 (一)c#Winform自定義控件-基類控件 查看this

開始

這個控件比較簡單,沒有多少東西,看下關鍵代碼吧url

 1  private int _value = 0;
 2 
 3         public int Value
 4         {
 5             get { return this._value; }
 6             set
 7             {
 8                 if (value < 0)
 9                     return;
10                 this._value = value;
11                 SetValue();
12             }
13         }
14 
15         private int maxValue = 100;
16 
17         public int MaxValue
18         {
19             get { return maxValue; }
20             set
21             {
22                 if (value <= 0)
23                     return;
24                 maxValue = value;
25                 SetValue();
26             }
27         }
28 
29         private void SetValue()
30         {
31             double dbl = (double)_value / (double)maxValue;
32             this.panel1.Width = (int)(this.Width * dbl);
33         }
34 
35         public ProcessExt()
36         {
37             InitializeComponent();
38         }
39 
40         private void ProcessExt_SizeChanged(object sender, EventArgs e)
41         {
42             SetValue();
43         }
44 
45         public void Step()
46         {
47             Value++;
48         }

而後看下完成代碼吧spa

 1 // 版權全部  黃正輝  交流羣:568015492   QQ:623128629
 2 // 文件名稱:ProcessExt.cs
 3 // 建立日期:2019-08-15 16:02:44
 4 // 功能描述:Process
 5 // 項目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
 6 using System;
 7 using System.Collections.Generic;
 8 using System.ComponentModel;
 9 using System.Drawing;
10 using System.Data;
11 using System.Linq;
12 using System.Text;
13 using System.Windows.Forms;
14 
15 namespace HZH_Controls.Controls
16 {
17     public partial class ProcessExt : UCControlBase
18     {
19         private int _value = 0;
20 
21         public int Value
22         {
23             get { return this._value; }
24             set
25             {
26                 if (value < 0)
27                     return;
28                 this._value = value;
29                 SetValue();
30             }
31         }
32 
33         private int maxValue = 100;
34 
35         public int MaxValue
36         {
37             get { return maxValue; }
38             set
39             {
40                 if (value <= 0)
41                     return;
42                 maxValue = value;
43                 SetValue();
44             }
45         }
46 
47         private void SetValue()
48         {
49             double dbl = (double)_value / (double)maxValue;
50             this.panel1.Width = (int)(this.Width * dbl);
51         }
52 
53         public ProcessExt()
54         {
55             InitializeComponent();
56         }
57 
58         private void ProcessExt_SizeChanged(object sender, EventArgs e)
59         {
60             SetValue();
61         }
62 
63         public void Step()
64         {
65             Value++;
66         }
67     }
68 }
View Code
 1 namespace HZH_Controls.Controls
 2 {
 3     partial class ProcessExt
 4     {
 5         /// <summary> 
 6         /// 必需的設計器變量。
 7         /// </summary>
 8         private System.ComponentModel.IContainer components = null;
 9 
10         /// <summary> 
11         /// 清理全部正在使用的資源。
12         /// </summary>
13         /// <param name="disposing">若是應釋放託管資源,爲 true;不然爲 false。</param>
14         protected override void Dispose(bool disposing)
15         {
16             if (disposing && (components != null))
17             {
18                 components.Dispose();
19             }
20             base.Dispose(disposing);
21         }
22 
23         #region 組件設計器生成的代碼
24 
25         /// <summary> 
26         /// 設計器支持所需的方法 - 不要
27         /// 使用代碼編輯器修改此方法的內容。
28         /// </summary>
29         private void InitializeComponent()
30         {
31             this.panel1 = new System.Windows.Forms.Panel();
32             this.SuspendLayout();
33             // 
34             // panel1
35             // 
36             this.panel1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(23)))), ((int)(((byte)(127)))), ((int)(((byte)(203)))));
37             this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
38             this.panel1.Location = new System.Drawing.Point(0, 0);
39             this.panel1.Name = "panel1";
40             this.panel1.Size = new System.Drawing.Size(0, 22);
41             this.panel1.TabIndex = 0;
42             // 
43             // ProcessExt
44             // 
45             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
46             this.BackColor = System.Drawing.Color.White;
47             this.ConerRadius = 5;
48             this.Controls.Add(this.panel1);
49             this.IsRadius = true;
50             this.Name = "ProcessExt";
51             this.Size = new System.Drawing.Size(291, 22);
52             this.SizeChanged += new System.EventHandler(this.ProcessExt_SizeChanged);
53             this.ResumeLayout(false);
54 
55         }
56 
57         #endregion
58 
59         private System.Windows.Forms.Panel panel1;
60     }
61 }
View Code

用處及效果

用處:就是一個進度條

效果:

最後的話

若是你喜歡的話,請到 https://gitee.com/kwwwvagaa/net_winform_custom_control 點個星 星吧

相關文章
相關標籤/搜索