前提 入行已經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
準備工作
此控制項用到了停靠窗體和日期控制項的一個面板,以及基類控制項,如果你還對此不瞭解,請移步
開始
添加一個用戶控制項,命名UCComboBox,繼承自UCControlBase
屬性
1 Color _ForeColor = Color.FromArgb(64, 64, 64); 2 [Description("文字顏色"), Category("自定義")] 3 public override Color ForeColor 4 { 5 get 6 { 7 return _ForeColor; 8 } 9 set 10 { 11 _ForeColor = value; 12 lblInput.ForeColor = value; 13 txtInput.ForeColor = value; 14 } 15 } 16 17 public event EventHandler SelectedChangedEvent; 18 public event EventHandler TextChangedEvent; 19 20 private ComboBoxStyle _BoxStyle = ComboBoxStyle.DropDown; 21 22 [Description("控制項樣式"), Category("自定義")] 23 public ComboBoxStyle BoxStyle 24 { 25 get { return _BoxStyle; } 26 set 27 { 28 _BoxStyle = value; 29 if (value == ComboBoxStyle.DropDownList) 30 { 31 lblInput.Visible = true; 32 txtInput.Visible = false; 33 } 34 else 35 { 36 lblInput.Visible = false; 37 txtInput.Visible = true; 38 } 39 40 if (this._BoxStyle == ComboBoxStyle.DropDownList) 41 { 42 txtInput.BackColor = _BackColor; 43 base.FillColor = _BackColor; 44 base.RectColor = _BackColor; 45 } 46 else 47 { 48 txtInput.BackColor = Color.White; 49 base.FillColor = Color.White; 50 base.RectColor = Color.FromArgb(220, 220, 220); 51 } 52 } 53 } 54 55 private Font _Font = new Font("微軟雅黑", 12); 56 [Description("字體"), Category("自定義")] 57 public new Font Font 58 { 59 get { return _Font; } 60 set 61 { 62 _Font = value; 63 lblInput.Font = value; 64 txtInput.Font = value; 65 txtInput.PromptFont = value; 66 this.txtInput.Location = new Point(this.txtInput.Location.X, (this.Height - txtInput.Height) / 2); 67 this.lblInput.Location = new Point(this.lblInput.Location.X, (this.Height - lblInput.Height) / 2); 68 } 69 } 70 71 72 [Obsolete("不再可用的屬性")] 73 [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] 74 private new Color FillColor 75 { 76 get; 77 set; 78 } 79 80 [Obsolete("不再可用的屬性")] 81 [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] 82 private new Color RectColor 83 { 84 get; 85 set; 86 } 87 88 private string _TextValue; 89 90 public string TextValue 91 { 92 get { return _TextValue; } 93 set 94 { 95 _TextValue = value; 96 if (lblInput.Text != value) 97 lblInput.Text = value; 98 if (txtInput.Text != value) 99 txtInput.Text = value; 100 } 101 } 102 103 private List<KeyValuePair<string, string>> _source = null; 104 105 public List<KeyValuePair<string, string>> Source 106 { 107 get { return _source; } 108 set 109 { 110 _source = value; 111 _selectedIndex = -1; 112 _selectedValue = ""; 113 _selectedItem = new KeyValuePair<string, string>(); 114 _selectedText = ""; 115 lblInput.Text = ""; 116 txtInput.Text = ""; 117 } 118 } 119 120 private KeyValuePair<string, string> _selectedItem = new KeyValuePair<string, string>(); 121 122 private int _selectedIndex = -1; 123 124 public int SelectedIndex 125 { 126 get 127 { 128 return _selectedIndex; 129 } 130 set 131 { 132 if (value < 0 || _source == null || _source.Count <= 0 || value >= _source.Count) 133 { 134 _selectedIndex = -1; 135 _selectedValue = ""; 136 _selectedItem = new KeyValuePair<string, string>(); 137 SelectedText = ""; 138 } 139 else 140 { 141 _selectedIndex = value; 142 _selectedItem = _source[value]; 143 _selectedValue = _source[value].Key; 144 SelectedText = _source[value].Value; 145 } 146 } 147 } 148 149 private string _selectedValue = ""; 150 151 public string SelectedValue 152 { 153 get 154 { 155 return _selectedValue; 156 } 157 set 158 { 159 if (_source == null || _source.Count <= 0) 160 { 161 SelectedText = ""; 162 _selectedValue = ""; 163 _selectedIndex = -1; 164 _selectedItem = new KeyValuePair<string, string>(); 165 } 166 else 167 { 168 for (int i = 0; i < _source.Count; i++) 169 { 170 if (_source[i].Key == value) 171 { 172 _selectedValue = value; 173 _selectedIndex = i; 174 _selectedItem = _source[i]; 175 SelectedText = _source[i].Value; 176 return; 177 } 178 } 179 _selectedValue = ""; 180 _selectedIndex = -1; 181 _selectedItem = new KeyValuePair<string, string>(); 182 SelectedText = ""; 183 } 184 } 185 } 186 187 private string _selectedText = ""; 188 189 public string SelectedText 190 { 191 get { return _selectedText; } 192 private set 193 { 194 _selectedText = value; 195 lblInput.Text = _selectedText; 196 txtInput.Text = _selectedText; 197 if (SelectedChangedEvent != null) 198 { 199 SelectedChangedEvent(this, null); 200 } 201 } 202 } 203 204 private int _ItemWidth = 70; 205 206 public int ItemWidth 207 { 208 get { return _ItemWidth; } 209 set { _ItemWidth = value; } 210 } 211 212 private int _dropPanelHeight = -1; 213 214 public int DropPanelHeight 215 { 216 get { return _dropPanelHeight; } 217 set { _dropPanelHeight = value; } 218 } 219 [Obsolete("不再可用的屬性,如需要改變背景色,請使用BackColorExt")] 220 [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] 221 private new Color BackColor 222 { 223 get 224 { 225 return base.BackColor; 226 } 227 set 228 { 229 base.BackColor = Color.Transparent; 230 } 231 } 232 233 private Color _BackColor = Color.FromArgb(240, 240, 240); 234 235 public Color BackColorExt 236 { 237 get 238 { 239 return _BackColor; 240 } 241 set 242 { 243 if (value == Color.Transparent) 244 return; 245 _BackColor = value; 246 lblInput.BackColor = value; 247 248 if (this._BoxStyle == ComboBoxStyle.DropDownList) 249 { 250 txtInput.BackColor = value; 251 base.FillColor = value; 252 base.RectColor = value; 253 } 254 else 255 { 256 txtInput.BackColor = Color.White; 257 base.FillColor = Color.White; 258 base.RectColor = Color.FromArgb(220, 220, 220); 259 } 260 } 261 }
構造函數初始化處理
1 public UCComboBox() 2 { 3 InitializeComponent(); 4 lblInput.BackColor = _BackColor; 5 if (this._BoxStyle == ComboBoxStyle.DropDownList) 6 { 7 txtInput.BackColor = _BackColor; 8 base.FillColor = _BackColor; 9 base.RectColor = _BackColor; 10 } 11 else 12 { 13 txtInput.BackColor = Color.White; 14 base.FillColor = Color.White; 15 base.RectColor = Color.FromArgb(220, 220, 220); 16 } 17 base.BackColor = Color.Transparent; 18 }
一些事件處理
1 private void UCComboBox_SizeChanged(object sender, EventArgs e) 2 { 3 this.txtInput.Location = new Point(this.txtInput.Location.X, (this.Height - txtInput.Height) / 2); 4 this.lblInput.Location = new Point(this.lblInput.Location.X, (this.Height - lblInput.Height) / 2); 5 } 6 7 private void txtInput_TextChanged(object sender, EventArgs e) 8 { 9 TextValue = txtInput.Text; 10 if (TextChangedEvent != null) 11 { 12 TextChangedEvent(this, null); 13 } 14 } 15 16 private void click_MouseDown(object sender, MouseEventArgs e) 17 { 18 if (_frmAnchor == null || _frmAnchor.IsDisposed || _frmAnchor.Visible == false) 19 { 20 21 if (this.Source != null && this.Source.Count > 0) 22 { 23 int intRow = 0; 24 int intCom = 1; 25 var p = this.PointToScreen(this.Location); 26 while (true) 27 { 28 int intScreenHeight = Screen.PrimaryScreen.Bounds.Height; 29 if ((p.Y + this.Height + this.Source.Count / intCom * 50 < intScreenHeight || p.Y - this.Source.Count / intCom * 50 > 0) 30 && (_dropPanelHeight <= 0 ? true : (this.Source.Count / intCom * 50 <= _dropPanelHeight))) 31 { 32 intRow = this.Source.Count / intCom + (this.Source.Count % intCom != 0 ? 1 : 0); 33 break; 34 } 35 intCom++; 36 } 37 UCTimePanel ucTime = new UCTimePanel(); 38 ucTime.IsShowBorder = true; 39 int intWidth = this.Width / intCom; 40 if (intWidth < _ItemWidth) 41 intWidth = _ItemWidth; 42 Size size = new Size(intCom * intWidth, intRow * 50); 43 ucTime.Size = size; 44 ucTime.FirstEvent = true; 45 ucTime.SelectSourceEvent += ucTime_SelectSourceEvent; 46 ucTime.Row = intRow; 47 ucTime.Column = intCom; 48 List<KeyValuePair<string, string>> lst = new List<KeyValuePair<string, string>>(); 49 foreach (var item in this.Source) 50 { 51 lst.Add(new KeyValuePair<string, string>(item.Key, item.Value)); 52 } 53 ucTime.Source = lst; 54 55 ucTime.SetSelect(_selectedValue); 56 57 _frmAnchor = new Forms.FrmAnchor(this, ucTime); 58 _frmAnchor.Load += (a, b) => { (a as Form).Size = size; }; 59 60 _frmAnchor.Show(this.FindForm()); 61 62 } 63 } 64 else 65 { 66 _frmAnchor.Close(); 67 } 68 } 69 70 71 Forms.FrmAnchor _frmAnchor; 72 void ucTime_SelectSourceEvent(object sender, EventArgs e) 73 { 74 if (_frmAnchor != null && !_frmAnchor.IsDisposed && _frmAnchor.Visible) 75 { 76 SelectedValue = sender.ToString(); 77 _frmAnchor.Close(); 78 } 79 } 80 81 private void UCComboBox_Load(object sender, EventArgs e) 82 { 83 if (this._BoxStyle == ComboBoxStyle.DropDownList) 84 { 85 txtInput.BackColor = _BackColor; 86 base.FillColor = _BackColor; 87 base.RectColor = _BackColor; 88 } 89 else 90 { 91 txtInput.BackColor = Color.White; 92 base.FillColor = Color.White; 93 base.RectColor = Color.FromArgb(220, 220, 220); 94 } 95 96 //if (this.Parent != null && BackColor == Color.Transparent) 97 } 98 } 99 }
完整代碼如下
1 // 版權所有 黃正輝 交流群:568015492 QQ:623128629 2 // 文件名稱:UCComboBox.cs 3 // 創建日期:2019-08-15 15:58:51 4 // 功能描述:ComboBox 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 [DefaultEvent("SelectedChangedEvent")] 18 public partial class UCComboBox : UCControlBase 19 { 20 Color _ForeColor = Color.FromArgb(64, 64, 64); 21 [Description("文字顏色"), Category("自定義")] 22 public override Color ForeColor 23 { 24 get 25 { 26 return _ForeColor; 27 } 28 set 29 { 30 _ForeColor = value; 31 lblInput.ForeColor = value; 32 txtInput.ForeColor = value; 33 } 34 } 35 36 public event EventHandler SelectedChangedEvent; 37 public event EventHandler TextChangedEvent; 38 39 private ComboBoxStyle _BoxStyle = ComboBoxStyle.DropDown; 40 41 [Description("控制項樣式"), Category("自定義")] 42 public ComboBoxStyle BoxStyle 43 { 44 get { return _BoxStyle; } 45 set 46 { 47 _BoxStyle = value; 48 if (value == ComboBoxStyle.DropDownList) 49 { 50 lblInput.Visible = true; 51 txtInput.Visible = false; 52 } 53 else 54 { 55 lblInput.Visible = false; 56 txtInput.Visible = true; 57 } 58 59 if (this._BoxStyle == ComboBoxStyle.DropDownList) 60 { 61 txtInput.BackColor = _BackColor; 62 base.FillColor = _BackColor; 63 base.RectColor = _BackColor; 64 } 65 else 66 { 67 txtInput.BackColor = Color.White; 68 base.FillColor = Color.White; 69 base.RectColor = Color.FromArgb(220, 220, 220); 70 } 71 } 72 } 73 74 private Font _Font = new Font("微軟雅黑", 12); 75 [Description("字體"), Category("自定