前提 入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。 開源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control 如果覺得寫的還行,請點個 star 支持一下吧 歡迎前來交流探討: 企鵝群568015492 目錄 ...
前提
入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。
開源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
如果覺得寫的還行,請點個 star 支持一下吧
目錄
https://www.cnblogs.com/bfyx/p/11364884.html
準備工作
日期控制項將分為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 }
全部代碼
1 // 版權所有 黃正輝 交流群:568015492 QQ:623128629 2 // 文件名稱:UCTimePanel.cs 3 // 創建日期:2019-08-15 15:59:56 4 // 功能描述:DateTime 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 16 namespace HZH_Controls.Controls 17 { 18 [ToolboxItem(false)] 19 public partial class UCTimePanel : UserControl 20 { 21 public event EventHandler SelectSourceEvent; 22 private List<KeyValuePair<string, string>> source = null; 23 public bool FirstEvent { get; set; } 24 25 public List<KeyValuePair<string, string>> Source 26 { 27 get { return source; } 28 set 29 { 30 source = value; 31 SetSource(value); 32 } 33 } 34 35 private bool _IsShowBorder = false; 36 37 public bool IsShowBorder 38 { 39 get { return _IsShowBorder; } 40 set 41 { 42 _IsShowBorder = value; 43 ucSplitLine_H1.Visible = value; 44 ucSplitLine_H2.Visible = value; 45 ucSplitLine_V1.Visible = value; 46 ucSplitLine_V2.Visible = value; 47 } 48 } 49 50 UCBtnExt selectBtn; 51 /// <summary> 52 /// 選中按鈕 53 /// </summary> 54 public UCBtnExt SelectBtn 55 { 56 get { return selectBtn; } 57 set 58 { 59 if (selectBtn != null && !selectBtn.IsDisposed) 60 { 61 selectBtn.FillColor = System.Drawing.Color.White; 62 selectBtn.RectColor = System.Drawing.Color.White; 63 selectBtn.BtnForeColor = System.Drawing.Color.FromArgb(66, 66, 66); 64 } 65 bool blnEvent = FirstEvent ? true : (selectBtn != null); 66 selectBtn = value; 67 if (value != null) 68 { 69 selectBtn.FillColor = System.Drawing.Color.FromArgb(255, 77, 59); 70 selectBtn.RectColor = System.Drawing.Color.FromArgb(255, 77, 59); 71 selectBtn.BtnForeColor = System.Drawing.Color.White; 72 if (blnEvent && SelectSourceEvent != null) 73 SelectSourceEvent(selectBtn.Tag.ToStringExt(), null); 74 } 75 } 76 } 77 public UCTimePanel() 78 { 79 InitializeComponent(); 80 this.SizeChanged += UCTimePanel_SizeChanged; 81 } 82 83 void UCTimePanel_SizeChanged(object sender, EventArgs e) 84 { 85 86 } 87 88 private int row = 0; 89 90 public int Row 91 { 92 get { return row; } 93 set 94 { 95 row = value; 96 ReloadPanel(); 97 } 98 } 99 100 101 private int column = 0; 102 103 public int Column 104 { 105 get { return column; } 106 set 107 { 108 column = value; 109 ReloadPanel(); 110 } 111 } 112 113 private void UCTimePanel_Load(object sender, EventArgs e) 114 { 115 116 } 117 118 #region 設置面板數據源 119 /// <summary> 120 /// 功能描述:設置面板數據源 121 /// 作 者:HZH 122 /// 創建日期:2019-06-25 15:02:15 123 /// 任務編號:POS 124 /// </summary> 125 /// <param name="lstSource">lstSource</param> 126 public void SetSource(List<KeyValuePair<string, string>> lstSource) 127 { 128 try 129 { 130 ControlHelper.FreezeControl(this, true); 131 if (row <= 0 || column <= 0) 132 return; 133 if (Source != lstSource) 134 Source = lstSource; 135 int index = 0; 136 SelectBtn = null; 137 foreach (UCBtnExt btn in this.panMain.Controls) 138 { 139 if (lstSource != null && index < lstSource.Count) 140 { 141 btn.BtnText = lstSource[index].Value; 142 btn.Tag = lstSource[index].Key; 143 index++; 144 } 145 else 146 { 147 btn.BtnText = ""; 148 btn.Tag = null; 149 } 150 } 151 } 152 finally 153 { 154 ControlHelper.FreezeControl(this, false); 155 } 156 } 157 #endregion 158 /// <summary> 159 /// 設置選中項 160 /// </summary> 161 /// <param name="strKey"></param> 162 public void SetSelect(string strKey) 163 { 164 foreach (UCBtnExt item in this.panMain.Controls) 165 { 166 if (item.Tag != null && item.Tag.ToStringExt() == strKey) 167 { 168 SelectBtn = item; 169 return; 170 } 171 } 172 SelectBtn = new UCBtnExt(); 173 } 174 175 #region 重置面板 176 /// <summary> 177 /// 功能描述:重置面板 178 /// 作 者:HZH 179 /// 創建日期:2019-06-25 15:02:05 180 /// 任務編號:POS 181 /// </summary> 182 public void ReloadPanel() 183 { 184 if (row <= 0 || column <= 0) 185 return; 186 SelectBtn = null; 187 this.panMain.Controls.Clear(); 188 this.panMain.ColumnCount = column; 189 this.panMain.ColumnStyles.Clear(); 190 for (int i = 0; i < column; i++) 191 { 192 this.panMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); 193 } 194 195 this.panMain.RowCount = row; 196 this.panMain.RowStyles.Clear(); 197 for (int i = 0; i < row; i++) 198 { 199 this.panMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F)); 200 } 201 202 for (int i = 0; i < row; i++) 203 { 204 for (int j = 0; j < column; j++) 205 { 206 UCBtnExt btn = new UCBtnExt(); 207 btn.BackColor = System.Drawing.Color.Transparent; 208 btn.BtnBackColor = System.Drawing.Color.Transparent; 209 btn.BtnFont = new System.Drawing.Font("微軟雅黑", 13F); 210 btn.BtnForeColor = System.Drawing.Color.FromArgb(66, 66, 66); 211 btn.ConerRadius = 5; 212 btn.Dock = DockStyle.Fill; 213 btn.FillColor = System.Drawing.Color.White; 214 btn.Font = new System.Drawing.Font("微軟雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel); 215 btn.Cursor = Cursor.Current; 216 btn.IsShowRect = true; 217 btn.IsRadius = true; 218 btn.IsShowTips = false; 219 btn.Name = "btn_" + i + "_" + j; 220 btn.RectColor = System.Drawing.Color.White; 221 btn.RectWidth = 1; 222 btn.Width = this.Width; 223 btn.TabIndex = 0; 224 btn.TipsText = ""; 225 btn.BtnClick += btn_BtnClick; 226 this.panMain.Controls.Add(btn, j, i); 227 } 228 } 229 230 if (Source != null) 231 { 232 SetSource(Source); 233 } 234 } 235 #endregion 236 237 void btn_BtnClick(object sender, EventArgs e) 238 { 239 var btn = (UCBtnExt)sender; 240 if (btn.Tag == null) 241 return; 242 SelectBtn = btn; 243 } 244 } 245 }View Code