前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
官网:https://www.hzhcontrols.cn
GitHub:https://github.com/kwwwvagaa/NetWinformControl
如果觉得写的还行,请点个 star 支持一下吧
目录
c#Winform自定义控件-目录_c#winform自定义控件-有图标的按钮-优快云博客
准备工作
日期控件将分为3部分进行处理,分别是,列表、日期面板、输入控件
将用到停靠窗体和基类控件,如你还没有了解,请移步查看
开始
添加用户控件,命名UCTimePanel
属性
1 public event EventHandler SelectSourceEvent;
2 private List<KeyValuePair<string, string>> source = null;
3 public bool FirstEvent { get; set; }
4
5 public List<KeyValuePair<string, string>> Source
6 {
7 get { return source; }
8 set
9 {
10 source = value;
11 SetSource(value);
12 }
13 }
14
15 private bool _IsShowBorder = false;
16
17 public bool IsShowBorder
18 {
19 get { return _IsShowBorder; }
20 set
21 {
22 _IsShowBorder = value;
23 ucSplitLine_H1.Visible = value;
24 ucSplitLine_H2.Visible = value;
25 ucSplitLine_V1.Visible = value;
26 ucSplitLine_V2.Visible = value;
27 }
28 }
29
30 UCBtnExt selectBtn;
31 /// <summary>
32 /// 选中按钮
33 /// </summary>
34 public UCBtnExt SelectBtn
35 {
36 get { return selectBtn; }
37 set
38 {
39 if (selectBtn != null && !selectBtn.IsDisposed)
40 {
41 selectBtn.FillColor = System.Drawing.Color.White;
42 selectBtn.RectColor = System.Drawing.Color.White;
43 selectBtn.BtnForeColor = System.Drawing.Color.FromArgb(66, 66, 66);
44 }
45 bool blnEvent = FirstEvent ? true : (selectBtn != null);
46 selectBtn = value;
47 if (value != null)
48 {
49 selectBtn.FillColor = System.Drawing.Color.FromArgb(255, 77, 59);
50 selectBtn.RectColor = System.Drawing.Color.FromArgb(255, 77, 59);
51 selectBtn.BtnForeColor = System.Drawing.Color.White;
52 if (blnEvent && SelectSourceEvent != null)
53 SelectSourceEvent(selectBtn.Tag.ToStringExt(), null);
54 }
55 }
56 }
57 private int row = 0;
58
59 public int Row
60 {
61 get { return row; }
62 set
63 {
64 row = value;
65 ReloadPanel();
66 }
67 }
68
69
70 private int column = 0;
71
72 public int Column
73 {
74 get { return column; }
75 set
76 {
77 column = value;
78 ReloadPanel();
79 }
80 }
一些公共函数
1 #region 设置面板数据源
2 /// <summary>
3 /// 功能描述:设置面板数据源
4 /// 作 者:HZH
5 /// 创建日期:2019-06-25 15:02:15
6 /// 任务编号:POS
7 /// </summary>
8 /// <param name="lstSource">lstSource</param>
9 public void SetSource(List<KeyValuePair<string, string>> lstSource)
10 {
11 try
12 {
13 ControlHelper.FreezeControl(this, true);
14 if (row <= 0 || column <= 0)
15 return;
16 if (Source != lstSource)
17 Source = lstSource;
18 int index = 0;
19 SelectBtn = null;
20 foreach (UCBtnExt btn in this.panMain.Controls)
21 {
22 if (lstSource != null && index < lstSource.Count)
23 {
24 btn.BtnText = lstSource[index].Value;
25 btn.Tag = lstSource[index].Key;
26 index++;
27 }
28 else
29 {
30 btn.BtnText = "";
31 btn.Tag = null;
32 }
33 }
34 }
35 finally
36 {
37 ControlHelper.FreezeControl(this, false);
38 }
39 }
40 #endregion
41 /// <summary>
42 /// 设置选中项
43 /// </summary>
44 /// <param name="strKey"></param>
45 public void SetSelect(string strKey)
46 {
47 foreach (UCBtnExt item in this.panMain.Controls)
48 {
49 if (item.Tag != null && item.Tag.ToStringExt() == strKey)
50 {
51 SelectBtn = item;
52 return;
53 }
54 }
55 SelectBtn = new UCBtnExt();
56 }
57
58 #region 重置面板
59 /// <summary>
60 /// 功能描述:重置面板
61 /// 作 者:HZH
62 /// 创建日期:2019-06-25 15:02:05
63 /// 任务编号:POS
64 /// </summary>
65 public void ReloadPanel()
66 {
67 if (row <= 0 || column <= 0)
68 return;
69 SelectBtn = null;
70 this.panMain.Controls.Clear();
71 this.panMain.ColumnCount = column;
72 this.panMain.ColumnStyles.Clear();
73 for (int i = 0; i < column; i++)
74 {
75 this.panMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
76 }
77
78 this.panMain.RowCount = row;
79 this.panMain.RowStyles.Clear();
80 for (int i = 0; i < row; i++)
81 {
82 this.panMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
83 }
84
85 for (int i = 0; i < row; i++)
86 {
87 for (int j = 0; j < column; j++)
88 {
89 UCBtnExt btn = new UCBtnExt();
90 btn.BackColor = System.Drawing.Color.Transparent;
91 btn.BtnBackColor = System.Drawing.Color.Transparent;
92 btn.BtnFont = new System.Drawing.Font("微软雅黑", 13F);
93 btn.BtnForeColor = System.Drawing.Color.FromArgb(66, 66, 66);
94 btn.ConerRadius = 5;
95 btn.Dock = DockStyle.Fill;
96 btn.FillColor = System.Drawing.Color.White;
97 btn.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
98 btn.Cursor = Cursor.Current;
99 btn.IsShowRect = true;
100 btn.IsRadius = true;
101 btn.IsShowTips = false;
102 btn.Name = "btn_" + i + "_" + j;
103 btn.RectColor = System.Drawing.Color.White;
104 btn.RectWidth = 1;
105 btn.Width = this.Width;
106 btn.TabIndex = 0;
107 btn.TipsText = "";
108 btn.BtnClick += btn_BtnClick;
109 this.panMain.Controls.Add(btn, j, i);
110 }
111 }
112
113 if (Source != null)
114 {
115 SetSource(Source);
116 }
117 }
118 #endregion
119
120 void btn_BtnClick(object sender, EventArgs e)
121 {
122 var btn = (UCBtnExt)sender;
123 if (btn.Tag == null)
124 return;
125 SelectBtn = btn;
126 }
全部代码
// 版权所有 黄正辉 交流群:568015492 QQ:623128629
// 文件名称:UCTimePanel.cs
// 创建日期:2019-08-15 15:59:56
// 功能描述:DateTime
// 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HZH_Controls.Controls
{
[ToolboxItem(false)]
public partial class UCTimePanel : UserControl
{
public event EventHandler SelectSourceEvent;
private List<KeyValuePair<string, string>> source = null;
public bool FirstEvent { get; set; }
public List<KeyValuePair<string, string>> Source
{
get { return source; }
set
{
source = value;
SetSource(value);
}
}
private bool _IsShowBorder = false;
public bool IsShowBorder
{
get { return _IsShowBorder; }
set
{
_IsShowBorder = value;
ucSplitLine_H1.Visible = value;
ucSplitLine_H2.Visible = value;
ucSplitLine_V1.Visible = value;
ucSplitLine_V2.Visible = value;
}
}
UCBtnExt selectBtn;
/// <summary>
/// 选中按钮
/// </summary>
public UCBtnExt SelectBtn
{
get { return selectBtn; }
set
{
if (selectBtn != null && !selectBtn.IsDisposed)
{
selectBtn.FillColor = System.Drawing.Color.White;
selectBtn.RectColor = System.Drawing.Color.White;
selectBtn.BtnForeColor = System.Drawing.Color.FromArgb(66, 66, 66);
}
bool blnEvent = FirstEvent ? true : (selectBtn != null);
selectBtn = value;
if (value != null)
{
selectBtn.FillColor = System.Drawing.Color.FromArgb(255, 77, 59);
selectBtn.RectColor = System.Drawing.Color.FromArgb(255, 77, 59);
selectBtn.BtnForeColor = System.Drawing.Color.White;
if (blnEvent && SelectSourceEvent != null)
SelectSourceEvent(selectBtn.Tag.ToStringExt(), null);
}
}
}
public UCTimePanel()
{
InitializeComponent();
this.SizeChanged += UCTimePanel_SizeChanged;
}
void UCTimePanel_SizeChanged(object sender, EventArgs e)
{
}
private int row = 0;
public int Row
{
get { return row; }
set
{
row = value;
ReloadPanel();
}
}
private int column = 0;
public int Column
{
get { return column; }
set
{
column = value;
ReloadPanel();
}
}
private void UCTimePanel_Load(object sender, EventArgs e)
{
}
#region 设置面板数据源
/// <summary>
/// 功能描述:设置面板数据源
/// 作 者:HZH
/// 创建日期:2019-06-25 15:02:15
/// 任务编号:POS
/// </summary>
/// <param name="lstSource">lstSource</param>
public void SetSource(List<KeyValuePair<string, string>> lstSource)
{
try
{
ControlHelper.FreezeControl(this, true);
if (row <= 0 || column <= 0)
return;
if (Source != lstSource)
Source = lstSource;
int index = 0;
SelectBtn = null;
foreach (UCBtnExt btn in this.panMain.Controls)
{
if (lstSource != null && index < lstSource.Count)
{
btn.BtnText = lstSource[index].Value;
btn.Tag = lstSource[index].Key;
index++;
}
else
{
btn.BtnText = "";
btn.Tag = null;
}
}
}
finally
{
ControlHelper.FreezeControl(this, false);
}
}
#endregion
/// <summary>
/// 设置选中项
/// </summary>
/// <param name="strKey"></param>
public void SetSelect(string strKey)
{
foreach (UCBtnExt item in this.panMain.Controls)
{
if (item.Tag != null && item.Tag.ToStringExt() == strKey)
{
SelectBtn = item;
return;
}
}
SelectBtn = new UCBtnExt();
}
#region 重置面板
/// <summary>
/// 功能描述:重置面板
/// 作 者:HZH
/// 创建日期:2019-06-25 15:02:05
/// 任务编号:POS
/// </summary>
public void ReloadPanel()
{
if (row <= 0 || column <= 0)
return;
SelectBtn = null;
this.panMain.Controls.Clear();
this.panMain.ColumnCount = column;
this.panMain.ColumnStyles.Clear();
for (int i = 0; i < column; i++)
{
this.panMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
}
this.panMain.RowCount = row;
this.panMain.RowStyles.Clear();
for (int i = 0; i < row; i++)
{
this.panMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
UCBtnExt btn = new UCBtnExt();
btn.BackColor = System.Drawing.Color.Transparent;
btn.BtnBackColor = System.Drawing.Color.Transparent;
btn.BtnFont = new System.Drawing.Font("微软雅黑", 13F);
btn.BtnForeColor = System.Drawing.Color.FromArgb(66, 66, 66);
btn.ConerRadius = 5;
btn.Dock = DockStyle.Fill;
btn.FillColor = System.Drawing.Color.White;
btn.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
btn.Cursor = Cursor.Current;
btn.IsShowRect = true;
btn.IsRadius = true;
btn.IsShowTips = false;
btn.Name = "btn_" + i + "_" + j;
btn.RectColor = System.Drawing.Color.White;
btn.RectWidth = 1;
btn.Width = this.Width;
btn.TabIndex = 0;
btn.TipsText = "";
btn.BtnClick += btn_BtnClick;
this.panMain.Controls.Add(btn, j, i);
}
}
if (Source != null)
{
SetSource(Source);
}
}
#endregion
void btn_BtnClick(object sender, EventArgs e)
{
var btn = (UCBtnExt)sender;
if (btn.Tag == null)
return;
SelectBtn = btn;
}
}
}
namespace HZH_Controls.Controls
{
partial class UCTimePanel
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.panMain = new System.Windows.Forms.TableLayoutPanel();
this.ucSplitLine_V1 = new HZH_Controls.Controls.UCSplitLine_V();
this.ucSplitLine_V2 = new HZH_Controls.Controls.UCSplitLine_V();
this.ucSplitLine_H1 = new HZH_Controls.Controls.UCSplitLine_H();
this.ucSplitLine_H2 = new HZH_Controls.Controls.UCSplitLine_H();
this.SuspendLayout();
//
// panMain
//
this.panMain.ColumnCount = 1;
this.panMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.panMain.Dock = System.Windows.Forms.DockStyle.Fill;
this.panMain.Location = new System.Drawing.Point(1, 1);
this.panMain.Name = "panMain";
this.panMain.RowCount = 1;
this.panMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.panMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.panMain.Size = new System.Drawing.Size(99, 228);
this.panMain.TabIndex = 0;
//
// ucSplitLine_V1
//
this.ucSplitLine_V1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
this.ucSplitLine_V1.Dock = System.Windows.Forms.DockStyle.Left;
this.ucSplitLine_V1.Location = new System.Drawing.Point(0, 0);
this.ucSplitLine_V1.Name = "ucSplitLine_V1";
this.ucSplitLine_V1.Size = new System.Drawing.Size(1, 230);
this.ucSplitLine_V1.TabIndex = 0;
this.ucSplitLine_V1.TabStop = false;
this.ucSplitLine_V1.Visible = false;
//
// ucSplitLine_V2
//
this.ucSplitLine_V2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
this.ucSplitLine_V2.Dock = System.Windows.Forms.DockStyle.Right;
this.ucSplitLine_V2.Location = new System.Drawing.Point(100, 0);
this.ucSplitLine_V2.Name = "ucSplitLine_V2";
this.ucSplitLine_V2.Size = new System.Drawing.Size(1, 230);
this.ucSplitLine_V2.TabIndex = 1;
this.ucSplitLine_V2.TabSto