(五)c#Winform自定义控件-复选框-HZHControls

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

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

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

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

欢迎前来交流探讨: 企鹅群568015492 

企鹅群568015492

目录

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

准备工作

准备4个图片,分别对应选中,没选中,选中禁用,没选中禁用

(五)c#Winform自定义控件-复选框-HZHControls

开始

新增一个用户控件,命名UCCheckBox

属性如下

1 [Description("选中改变事件"), Category("自定义")] 2 public event EventHandler CheckedChangeEvent; 3 4 [Description("字体"), Category("自定义")] 5 public override Font Font 6 { 7 get 8 { 9 return base.Font; 10 } 11 set 12 { 13 base.Font = value; 14 label1.Font = value; 15 } 16 } 17 18 private Color _ForeColor = Color.FromArgb(62, 62, 62); 19 [Description("字体颜色"), Category("自定义")] 20 public new Color ForeColor 21 { 22 get { return _ForeColor; } 23 set 24 { 25 label1.ForeColor = value; 26 _ForeColor = value; 27 } 28 } 29 private string _Text = "复选框"; 30 [Description("文本"), Category("自定义")] 31 public string TextValue 32 { 33 get { return _Text; } 34 set 35 { 36 label1.Text = value; 37 _Text = value; 38 } 39 } 40 private bool _checked = false; 41 [Description("是否选中"), Category("自定义")] 42 public bool Checked 43 { 44 get 45 { 46 return _checked; 47 } 48 set 49 { 50 if (_checked != value) 51 { 52 _checked = value; 53 if (base.Enabled) 54 { 55 if (_checked) 56 { 57 panel1.BackgroundImage = Properties.Resources.checkbox1; 58 } 59 else 60 { 61 panel1.BackgroundImage = Properties.Resources.checkbox0; 62 } 63 } 64 else 65 { 66 if (_checked) 67 { 68 panel1.BackgroundImage = Properties.Resources.checkbox10; 69 } 70 else 71 { 72 panel1.BackgroundImage = Properties.Resources.checkbox00; 73 } 74 } 75 76 if (CheckedChangeEvent != null) 77 { 78 CheckedChangeEvent(this, null); 79 } 80 } 81 } 82 } 83 84 public new bool Enabled 85 { 86 get 87 { 88 return base.Enabled; 89 } 90 set 91 { 92 base.Enabled = value; 93 if (value) 94 { 95 if (_checked) 96 { 97 panel1.BackgroundImage = Properties.Resources.checkbox1; 98 } 99 else 100 { 101 panel1.BackgroundImage = Properties.Resources.checkbox0; 102 } 103 } 104 else 105 { 106 if (_checked) 107 { 108 panel1.BackgroundImage = Properties.Resources.checkbox10; 109 } 110 else 111 { 112 panel1.BackgroundImage = Properties.Resources.checkbox00; 113 } 114 } 115 } 116 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzpgxj.html