前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
<https://gitee.com/kwwwvagaa/net_winform_custom_control>

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492  <https://shang.qq.com/wpa/qunwpa?
idkey=6e08741ef16fe53bf0314c1c9e336c4f626047943a8b76bac062361bab6b4f8d>

目录

https://www.cnblogs.com/bfyx/p/11364884.html
<https://www.cnblogs.com/bfyx/p/11364884.html>

准备工作

该控件将继承基类控件UCControlBase,如果你还对UCControlBase不了解,请移步 (一)c#Winform自定义控件-基类控件
<https://www.cnblogs.com/bfyx/p/11361809.html> 查看

开始

这个控件比较简单,没有多少东西,看下关键代码吧
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
MaxValue18 { 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 }
然后看下完成代码吧
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 //
ProcessExt44 // 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
<https://gitee.com/kwwwvagaa/net_winform_custom_control> 点个星 星吧

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信