(三十五)c#Winform自定义控件-下拉框

前提

入行已经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

准备工作

此控件用到了停靠窗体和日期控件的一个面板,以及基类控件,如果你还对此不了解,请移步

(一)c#Winform自定义控件-基类控件

(十九)c#Winform自定义控件-停靠窗体

(三十三)c#Winform自定义控件-日期控件

开始

添加一个用户控件,命名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("自定义")]
 76         public new Font Font
 77         {
 78             get { return _Font; }
 79             set
 80             {
 81                 _Font = value;
 82                 lblInput.Font = value;
 83                 txtInput.Font = value;
 84                 txtInput.PromptFont = value;
 85                 this.txtInput.Location = new Point(this.txtInput.Location.X, (this.Height - txtInput.Height) / 2);
 86                 this.lblInput.Location = new Point(this.lblInput.Location.X, (this.Height - lblInput.Height) / 2);
 87             }
 88         }
 89 
 90 
 91         [Obsolete("不再可用的属性")]
 92         [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
 93         private new Color FillColor
 94         {
 95             get;
 96             set;
 97         }
 98 
 99         [Obsolete("不再可用的属性")]
100         [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
101         private new Color RectColor
102         {
103             get;
104             set;
105         }
106 
107         private string _TextValue;
108 
109         public string TextValue
110         {
111             get { return _TextValue; }
112             set
113             {
114                 _TextValue = value;
115                 if (lblInput.Text != value)
116                     lblInput.Text = value;
117                 if (txtInput.Text != value)
118                     txtInput.Text = value;
119             }
120         }
121 
122         private List<KeyValuePair<string, string>> _source = null;
123 
124         public List<KeyValuePair<string, string>> Source
125         {
126             get { return _source; }
127             set
128             {
129                 _source = value;
130                 _selectedIndex = -1;
131                 _selectedValue = "";
132                 _selectedItem = new KeyValuePair<string, string>();
133                 _selectedText = "";
134                 lblInput.Text = "";
135                 txtInput.Text = "";
136             }
137         }
138 
139         private KeyValuePair<string, string> _selectedItem = new KeyValuePair<string, string>();
140 
141         private int _selectedIndex = -1;
142 
143         public int SelectedIndex
144         {
145             get
146             {
147                 return _selectedIndex;
148             }
149             set
150             {
151                 if (value < 0 || _source == null || _source.Count <= 0 || value >= _source.Count)
152                 {
153                     _selectedIndex = -1;
154                     _selectedValue = "";
155                     _selectedItem = new KeyValuePair<string, string>();
156                     SelectedText = "";
157                 }
158                 else
159                 {
160                     _selectedIndex = value;
161                     _selectedItem = _source[value];
162                     _selectedValue = _source[value].Key;
163                     SelectedText = _source[value].Value;
164                 }
165             }
166         }
167 
168         private string _selectedValue = "";
169 
170         public string SelectedValue
171         {
172             get
173             {
174                 return _selectedValue;
175             }
176             set
177             {
178                 if (_source == null || _source.Count <= 0)
179                 {
180                     SelectedText = "";
181                     _selectedValue = "";
182                     _selectedIndex = -1;
183                     _selectedItem = new KeyValuePair<string, string>();
184                 }
185                 else
186                 {
187                     for (int i = 0; i < _source.Count; i++)
188                     {
189                         if (_source[i].Key == value)
190                         {
191                             _selectedValue = value;
192                             _selectedIndex = i;
193                             _selectedItem = _source[i];
194                             SelectedText = _source[i].Value;
195                             return;
196                         }
197                     }
198                     _selectedValue = "";
199                     _selectedIndex = -1;
200                     _selectedItem = new KeyValuePair<string, string>();
201                     SelectedText = "";
202                 }
203             }
204         }
205 
206         private string _selectedText = "";
207 
208         public string SelectedText
209         {
210             get { return _selectedText; }
211             private set
212             {
213                 _selectedText = value;
214                 lblInput.Text = _selectedText;
215                 txtInput.Text = _selectedText;
216                 if (SelectedChangedEvent != null)
217                 {
218                     SelectedChangedEvent(this, null);
219                 }
220             }
221         }
222 
223         private int _ItemWidth = 70;
224 
225         public int ItemWidth
226         {
227             get { return _ItemWidth; }
228             set { _ItemWidth = value; }
229         }
230 
231         private int _dropPanelHeight = -1;
232 
233         public int DropPanelHeight
234         {
235             get { return _dropPanelHeight; }
236             set { _dropPanelHeight = value; }
237         }
238         [Obsolete("不再可用的属性,如需要改变背景色,请使用BackColorExt")]
239         [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
240         private new Color BackColor
241         {
242             get
243             {
244                 return base.BackColor;
245             }
246             set
247             {
248                 base.BackColor = Color.Transparent;
249             }
250         }
251 
252         private Color _BackColor = Color.FromArgb(240, 240, 240);
253 
254         public Color BackColorExt
255         {
256             get
257             {
258                 return _BackColor;
259             }
260             set
261             {
262                 if (value == Color.Transparent)
263                     return;
264                 _BackColor = value;
265                 lblInput.BackColor = value;
266 
267                 if (this._BoxStyle == ComboBoxStyle.DropDownList)
268                 {
269                     txtInput.BackColor = value;
270                     base.FillColor = value;
271                     base.RectColor = value;
272                 }
273                 else
274                 {
275                     txtInput.BackColor = Color.White;
276                     base.FillColor = Color.White;
277                     base.RectColor = Color.FromArgb(220, 220, 220);
278                 }
279             }
280         }
281 
282         public UCComboBox()
283         {
284             InitializeComponent();
285             lblInput.BackColor = _BackColor;
286             if (this._BoxStyle == ComboBoxStyle.DropDownList)
287             {
288                 txtInput.BackColor = _BackColor;
289                 base.FillColor = _BackColor;
290                 base.RectColor = _BackColor;
291             }
292             else
293             {
294                 txtInput.BackColor = Color.White;
295                 base.FillColor = Color.White;
296                 base.RectColor = Color.FromArgb(220, 220, 220);
297             }
298             base.BackColor = Color.Transparent;
299         }
300 
301         private void UCComboBox_SizeChanged(object sender, EventArgs e)
302         {
303             this.txtInput.Location = new Point(this.txtInput.Location.X, (this.Height - txtInput.Height) / 2);
304             this.lblInput.Location = new Point(this.lblInput.Location.X, (this.Height - lblInput.Height) / 2);
305         }
306 
307         private void txtInput_TextChanged(object sender, EventArgs e)
308         {
309             TextValue = txtInput.Text;
310             if (TextChangedEvent != null)
311             {
312                 TextChangedEvent(this, null);
313             }
314         }
315 
316         private void click_MouseDown(object sender, MouseEventArgs e)
317         {
318             if (_frmAnchor == null || _frmAnchor.IsDisposed || _frmAnchor.Visible == false)
319             {
320 
321                 if (this.Source != null && this.Source.Count > 0)
322                 {
323                     int intRow = 0;
324                     int intCom = 1;
325                     var p = this.PointToScreen(this.Location);
326                     while (true)
327                     {
328                         int intScreenHeight = Screen.PrimaryScreen.Bounds.Height;
329                         if ((p.Y + this.Height + this.Source.Count / intCom * 50 < intScreenHeight || p.Y - this.Source.Count / intCom * 50 > 0)
330                             && (_dropPanelHeight <= 0 ? true : (this.Source.Count / intCom * 50 <= _dropPanelHeight)))
331                         {
332                             intRow = this.Source.Count / intCom + (this.Source.Count % intCom != 0 ? 1 : 0);
333                             break;
334                         }
335                         intCom++;
336                     }
337                     UCTimePanel ucTime = new UCTimePanel();
338                     ucTime.IsShowBorder = true;
339                     int intWidth = this.Width / intCom;
340                     if (intWidth < _ItemWidth)
341                         intWidth = _ItemWidth;
342                     Size size = new Size(intCom * intWidth, intRow * 50);
343                     ucTime.Size = size;
344                     ucTime.FirstEvent = true;
345                     ucTime.SelectSourceEvent += ucTime_SelectSourceEvent;
346                     ucTime.Row = intRow;
347                     ucTime.Column = intCom;
348                     List<KeyValuePair<string, string>> lst = new List<KeyValuePair<string, string>>();
349                     foreach (var item in this.Source)
350                     {
351                         lst.Add(new KeyValuePair<string, string>(item.Key, item.Value));
352                     }
353                     ucTime.Source = lst;
354 
355                     ucTime.SetSelect(_selectedValue);
356 
357                     _frmAnchor = new Forms.FrmAnchor(this, ucTime);
358                     _frmAnchor.Load += (a, b) => { (a as Form).Size = size; };
359 
360                     _frmAnchor.Show(this.FindForm());
361 
362                 }
363             }
364             else
365             {
366                 _frmAnchor.Close();
367             }
368         }
369 
370 
371         Forms.FrmAnchor _frmAnchor;
372         void ucTime_SelectSourceEvent(object sender, EventArgs e)
373         {
374             if (_frmAnchor != null && !_frmAnchor.IsDisposed && _frmAnchor.Visible)
375             {
376                 SelectedValue = sender.ToString();
377                 _frmAnchor.Close();
378             }
379         }
380 
381         private void UCComboBox_Load(object sender, EventArgs e)
382         {
383             if (this._BoxStyle == ComboBoxStyle.DropDownList)
384             {
385                 txtInput.BackColor = _BackColor;
386                 base.FillColor = _BackColor;
387                 base.RectColor = _BackColor;
388             }
389             else
390             {
391                 txtInput.BackColor = Color.White;
392                 base.FillColor = Color.White;
393                 base.RectColor = Color.FromArgb(220, 220, 220);
394             }
395         }
396     }
397 }
View Code
  1 namespace HZH_Controls.Controls
  2 {
  3     partial class UCComboBox
  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.txtInput = new HZH_Controls.Controls.TextBoxEx();
 33             this.lblInput = new System.Windows.Forms.Label();
 34             this.SuspendLayout();
 35             // 
 36             // panel1
 37             // 
 38             this.panel1.BackColor = System.Drawing.Color.Transparent;
 39             this.panel1.BackgroundImage = global::HZH_Controls.Properties.Resources.ComboBox;
 40             this.panel1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
 41             this.panel1.Dock = System.Windows.Forms.DockStyle.Right;
 42             this.panel1.Location = new System.Drawing.Point(136, 0);
 43             this.panel1.Name = "panel1";
 44             this.panel1.Size = new System.Drawing.Size(37, 32);
 45             this.panel1.TabIndex = 0;
 46             this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.click_MouseDown);
 47             // 
 48             // txtInput
 49             // 
 50             this.txtInput.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
 51             this.txtInput.BackColor = System.Drawing.Color.White;
 52             this.txtInput.BorderStyle = System.Windows.Forms.BorderStyle.None;
 53             this.txtInput.DecLength = 2;
 54             this.txtInput.Font = new System.Drawing.Font("微软雅黑", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
 55             this.txtInput.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
 56             this.txtInput.InputType = TextInputType.NotControl;
 57             this.txtInput.Location = new System.Drawing.Point(3, 4);
 58             this.txtInput.Margin = new System.Windows.Forms.Padding(3, 3, 10, 3);
 59             this.txtInput.MaxValue = new decimal(new int[] {
 60             1000000,
 61             0,
 62             0,
 63             0});
 64             this.txtInput.MinValue = new decimal(new int[] {
 65             1000000,
 66             0,
 67             0,
 68             -2147483648});
 69             this.txtInput.MyRectangle = new System.Drawing.Rectangle(0, 0, 0, 0);
 70             this.txtInput.Name = "txtInput";
 71             this.txtInput.OldText = null;
 72             this.txtInput.PromptColor = System.Drawing.Color.Silver;
 73             this.txtInput.PromptFont = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
 74             this.txtInput.PromptText = "";
 75             this.txtInput.RegexPattern = "";
 76             this.txtInput.Size = new System.Drawing.Size(133, 24);
 77             this.txtInput.TabIndex = 1;
 78             this.txtInput.TextChanged += new System.EventHandler(this.txtInput_TextChanged);
 79             // 
 80             // lblInput
 81             // 
 82             this.lblInput.AutoSize = true;
 83             this.lblInput.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
 84             this.lblInput.Location = new System.Drawing.Point(3, 6);
 85             this.lblInput.Name = "lblInput";
 86             this.lblInput.Size = new System.Drawing.Size(0, 20);
 87             this.lblInput.TabIndex = 2;
 88             this.lblInput.Visible = false;
 89             this.lblInput.MouseDown += new System.Windows.Forms.MouseEventHandler(this.click_MouseDown);
 90             // 
 91             // UCComboBox
 92             // 
 93             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
 94             this.BackColor = System.Drawing.Color.Transparent;
 95             this.ConerRadius = 5;
 96             this.Controls.Add(this.panel1);
 97             this.Controls.Add(this.txtInput);
 98             this.Controls.Add(this.lblInput);
 99             this.FillColor = System.Drawing.Color.Gainsboro;
100             this.IsShowRect = true;
101             this.Name = "UCComboBox";
102             this.Size = new System.Drawing.Size(173, 32);
103             this.Load += new System.EventHandler(this.UCComboBox_Load);
104             this.SizeChanged += new System.EventHandler(this.UCComboBox_SizeChanged);
105             this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.click_MouseDown);
106             this.ResumeLayout(false);
107             this.PerformLayout();
108 
109         }
110 
111         #endregion
112 
113         private System.Windows.Forms.Panel panel1;
114         public TextBoxEx txtInput;
115         private System.Windows.Forms.Label lblInput;
116     }
117 }
View Code

用处及效果

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星 星吧

posted @ 2019-08-17 10:56 冰封一夏 阅读(...) 评论(...) 编辑 收藏