整合TextBox与Label 创建新控件--EFLabelText
程序开发中,经常用到一个Label 和一个TextBox组合显示数据信息,为此要频繁的拉两个控件再做适当调整,为减少类似“傻瓜”作业,而写了这个新控件--EFLabelText,其就是组合Label和TextBox,程序员只要拉一次即可实现原来之较为繁琐的动作。没有技术难点,仅是繁琐点。以下贴出全部代码,稍作修改编译即可使用。(在VS2005 2.0下编译通过,可正常使用之)
程序开发中,经常用到一个Label 和一个TextBox组合显示数据信息,为此要频繁的拉两个控件再做适当调整,为减少类似“傻瓜”作业,而写了这个新控件--EFLabelText,其就是组合Label和TextBox,程序员只要拉一次即可实现原来之较为繁琐的动作。没有技术难点,仅是繁琐点。以下贴出全部代码,稍作修改编译即可使用。(在VS2005 2.0下编译通过,可正常使用之)
Code
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Drawing;
5 using System.Data;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Web.UI.Design.WebControls;
9 using System.Drawing.Design;
10
11 namespace EF
12 {
13 public partial class EFLabelText : UserControl
14 {
15 // Fields
16 private ValueType item_type;
17 private ArrangeMode m_nArrange;
18 private bool m_bArrangeChanging;
19 private bool m_bDynamicSize;
20 private string item_ename;
21 private int m_nLabelHorizontalLength;
22 private int m_nTextBoxHorizontalLength;
23
24 // Events
25 [Category("EF事件"),Description("对回车进行处理")]
26 public event EFEnterPressEventHandler DoEnterPress;
27
28 [Category("EF事件"),Description("当用户输入不匹配当前定义的正则表达式时触发")]
29 public event EventHandler EFTextInvalid
30 {
31 add
32 {
33 this.efTextBox1.EFTextInvalid += value;
34 }
35 remove
36 {
37 this.efTextBox1.EFTextInvalid -= value;
38 }
39 }
40
41 // Methods
42 public EFLabelText()
43 {
44 this.m_bDynamicSize = false;
45 this.m_bArrangeChanging = false;
46 this.InitializeComponent();
47 this.efLabel1.in_labeltext = true;
48 this.m_nArrange = ArrangeMode.horizontal;
49 this.item_type = ValueType.EFString;
50 base.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
51 this.efTextBox1.DoEnterPress += new EFTextBox.EFEnterPressEventHandler(this.OnDoEnterPress);
52 this.BackColor = Color.Transparent;
53 }
54
55 public EFLabelText(bool DynamicSize)
56 {
57 this.m_bDynamicSize = DynamicSize;
58 this.m_bArrangeChanging = false;
59 this.InitializeComponent();
60 this.efLabel1.in_labeltext = true;
61 this.m_nArrange = ArrangeMode.horizontal;
62 this.item_type = ValueType.EFString;
63 base.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
64 this.BackColor = Color.Transparent;
65 }
66
67 private void ArrangeChange()
68 {
69 if (this.m_nArrange == ArrangeMode.horizontal)
70 {
71 this.efLabel1.Top = 0;
72 this.efLabel1.Left = 0;
73 this.efTextBox1.Top = 0;
74 base.Height = this.efTextBox1.Height;
75 if (this.m_bArrangeChanging)
76 {
77 this.efLabel1.Width = this.m_nLabelHorizontalLength;
78 this.efLabel1.Height = this.efTextBox1.Height;
79 this.efTextBox1.Left = this.m_nLabelHorizontalLength + 1;
80 }
81 else
82 {
83 this.efTextBox1.Width = (base.Width - this.efLabel1.Width) - 2;
84 this.efTextBox1.Left = this.efLabel1.Width + 1;
85 }
86 }
87 else
88 {
89 this.efLabel1.Left = 0;
90 this.efLabel1.Top = 0;
91 this.efTextBox1.Left = 0;
92 if (this.m_bArrangeChanging)
93 {
94 base.Width = (this.efLabel1.Width > this.efTextBox1.Width) ? this.efLabel1.Width : this.efTextBox1.Width;
95 base.Height = (this.efLabel1.Height + this.efTextBox1.Height) + 2;
96 }
97 else
98 {
99 this.efLabel1.Height = (base.Height - this.efTextBox1.Height) - 2;
100 this.efTextBox1.Top = this.efLabel1.Height + 2;
101 }
102 this.efLabel1.Width = base.Width;
103 this.efTextBox1.Width = base.Width;
104 }
105 this.m_bArrangeChanging = false;
106 }
107
108 private void EFLabelText_Load(object sender, EventArgs e)
109 {
110 if (this.EFCname == null)
111 {
112 this.EFCname = base.Name;
113 }
114 }
115
116 private void EFLabelText_SizeChanged(object sender, EventArgs e)
117 {
118 this.ArrangeChange();
119 }
120
121 public bool EFSetFocus()
122 {
123 return this.efTextBox1.Focus();
124 }
125
126 private void efTextBox1_EFTextInvalid(object sender, EventArgs e)
127 {
128 this.errorProvider1.SetError(this, "格式输入错误,格式符为" + this.EFLeaveExpression + "!");
129 }
130
131 private void efTextBox1_KeyPress(object sender, KeyPressEventArgs e)
132 {
133 int keyChar = e.KeyChar;
134 if (this.item_type != ValueType.EFString)
135 {
136 if (this.item_type == ValueType.EFDecimal)
137 {
138 }
139 if (((keyChar < 0x30) || (keyChar > 0x39)) && ((keyChar != 0x2e) && (keyChar != 8)))
140 {
141 e.Handled = true;
142 }
143 }
144 }
145
146 private void efTextBox1_TextChanged(object sender, EventArgs e)
147 {
148 this.errorProvider1.SetError(this, "");
149 }
150
151 public Size getSize()
152 {
153 if (this.m_nArrange == ArrangeMode.horizontal)
154 {
155 return new Size((this.efTextBox1.Size.Width + this.efLabel1.Size.Width) + 2, base.Height);
156 }
157 return new Size(base.Width, this.efTextBox1.Size.Height + this.efLabel1.Size.Height);
158 }
159
160 protected virtual void OnDoEnterPress(object sender, EventArgs e)
161 {
162 if (this.DoEnterPress != null)
163 {
164 this.DoEnterPress(this, e);
165 }
166 }
167
168 protected override void OnPaint(PaintEventArgs e)
169 {
170 Graphics graphics = e.Graphics;
171 SizeF ef = graphics.MeasureString(this.efLabel1.Text, this.Font);
172 this.m_nLabelHorizontalLength = Convert.ToInt32(ef.Width) + 4;
173 ef = graphics.MeasureString(this.efTextBox1.Text, this.Font);
174 this.m_nTextBoxHorizontalLength = Convert.ToInt32(ef.Width);
175 if (this.m_bDynamicSize)
176 {
177 this.efTextBox1.Width = (this.m_nTextBoxHorizontalLength < 40) ? 40 : this.m_nTextBoxHorizontalLength;
178 base.Width = (this.efTextBox1.Width + this.efLabel1.Width) + 2;
179 }
180 if (this.m_nArrange == ArrangeMode.horizontal)
181 {
182 this.efLabel1.Width = this.m_nLabelHorizontalLength;
183 }
184 this.ArrangeChange();
185 }
186
187 public override void Refresh()
188 {
189 base.Refresh();
190 }
191
192 // Properties
193 [Category("EF属性"), Description("组成方式")]
194 public ArrangeMode EFArrange
195 {
196 get
197 {
198 return this.m_nArrange;
199 }
200 set
201 {
202 this.m_nArrange = value;
203 this.m_bArrangeChanging = true;
204 this.Refresh();
205 }
206 }
207
208 [Category("EF属性"), Description("中文")]
209 public string EFCname
210 {
211 get
212 {
213 return this.efLabel1.Text;
214 }
215 set
216 {
217 this.efLabel1.Text = value;
218 this.Refresh();
219 }
220 }
221
222 [Category("EF属性"),Description("英文")]
223 public string EFEname
224 {
225 get
226 {
227 return this.item_ename;
228 }
229 set
230 {
231 this.item_ename = value;
232 }
233 }
234
235 [Category("EF属性"),Description("输入文字")]
236 public string EFEnterText
237 {
238 get
239 {
240 return this.efTextBox1.Text;
241 }
242 set
243 {
244 this.efTextBox1.Text = value;
245 }
246 }
247
248 [ Category("EF属性"),Description("获得标签引用")]
249 public EFLabel EFLabelRef
250 {
251 get
252 {
253 return this.efLabel1;
254 }
255 }
256
257 [Category("EF属性"), Description("上一次正确的值")]
258 public string EFLastValidValue
259 {
260 get
261 {
262 return this.efTextBox1.EFLastValidValue;
263 }
264 }
265
266 [Category("EF属性"), Editor(typeof(RegexTypeEditor), typeof(UITypeEditor)), Description("焦点离开文本框时进行匹配检查的正则表达式")]
267 public string EFLeaveExpression
268 {
269 get
270 {
271 return this.efTextBox1.EFLeaveExpression;
272 }
273 set
274 {
275 this.efTextBox1.EFLeaveExpression = value;
276 }
277 }
278
279 [Category("EF属性"),Description("长度")]
280 public int EFLen
281 {
282 get
283 {
284 if (this.item_type == ValueType.EFDecimal)
285 {
286 return this.efTextBox1.MaxLength;
287 }
288 return this.efTextBox1.MaxLength;
289 }
290 set
291 {
292 int num;
293 if (value <= 0)
294 {
295 num = 1;
296 }
297 else
298 {
299 num = value;
300 }
301 if (this.item_type == ValueType.EFDecimal)
302 {
303 this.efTextBox1.MaxLength = num;
304 }
305 else
306 {
307 this.efTextBox1.MaxLength = num;
308 }
309 }
310 }
311
312 [Category("EF属性"), Description("获得文本引用")]
313 public EFTextBox EFTextRef
314 {
315 get
316 {
317 return this.efTextBox1;
318 }
319 }
320
321 [Category("EF属性"), Description("类型")]
322 public ValueType EFType
323 {
324 get
325 {
326 return this.item_type;
327 }
328 set
329 {
330 this.item_type = value;
331 if (value == ValueType.EFDateTime)
332 {
333 this.EFLen = 14;
334 }
335 }
336 }
337
338 [Category("EF属性"), Description("大写字母")]
339 public bool EFUpperCase
340 {
341 get
342 {
343 return (this.efTextBox1.CharacterCasing == CharacterCasing.Upper);
344 }
345 set
346 {
347 if (value)
348 {
349 this.efTextBox1.CharacterCasing = CharacterCasing.Upper;
350 }
351 else
352 {
353 this.efTextBox1.CharacterCasing = CharacterCasing.Normal;
354 }
355 }
356 }
357
358 // Nested Types
359 public delegate void EFEnterPressEventHandler(object sender, EventArgs e);
360 }
361 }
362
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Drawing;
5 using System.Data;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Web.UI.Design.WebControls;
9 using System.Drawing.Design;
10
11 namespace EF
12 {
13 public partial class EFLabelText : UserControl
14 {
15 // Fields
16 private ValueType item_type;
17 private ArrangeMode m_nArrange;
18 private bool m_bArrangeChanging;
19 private bool m_bDynamicSize;
20 private string item_ename;
21 private int m_nLabelHorizontalLength;
22 private int m_nTextBoxHorizontalLength;
23
24 // Events
25 [Category("EF事件"),Description("对回车进行处理")]
26 public event EFEnterPressEventHandler DoEnterPress;
27
28 [Category("EF事件"),Description("当用户输入不匹配当前定义的正则表达式时触发")]
29 public event EventHandler EFTextInvalid
30 {
31 add
32 {
33 this.efTextBox1.EFTextInvalid += value;
34 }
35 remove
36 {
37 this.efTextBox1.EFTextInvalid -= value;
38 }
39 }
40
41 // Methods
42 public EFLabelText()
43 {
44 this.m_bDynamicSize = false;
45 this.m_bArrangeChanging = false;
46 this.InitializeComponent();
47 this.efLabel1.in_labeltext = true;
48 this.m_nArrange = ArrangeMode.horizontal;
49 this.item_type = ValueType.EFString;
50 base.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
51 this.efTextBox1.DoEnterPress += new EFTextBox.EFEnterPressEventHandler(this.OnDoEnterPress);
52 this.BackColor = Color.Transparent;
53 }
54
55 public EFLabelText(bool DynamicSize)
56 {
57 this.m_bDynamicSize = DynamicSize;
58 this.m_bArrangeChanging = false;
59 this.InitializeComponent();
60 this.efLabel1.in_labeltext = true;
61 this.m_nArrange = ArrangeMode.horizontal;
62 this.item_type = ValueType.EFString;
63 base.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
64 this.BackColor = Color.Transparent;
65 }
66
67 private void ArrangeChange()
68 {
69 if (this.m_nArrange == ArrangeMode.horizontal)
70 {
71 this.efLabel1.Top = 0;
72 this.efLabel1.Left = 0;
73 this.efTextBox1.Top = 0;
74 base.Height = this.efTextBox1.Height;
75 if (this.m_bArrangeChanging)
76 {
77 this.efLabel1.Width = this.m_nLabelHorizontalLength;
78 this.efLabel1.Height = this.efTextBox1.Height;
79 this.efTextBox1.Left = this.m_nLabelHorizontalLength + 1;
80 }
81 else
82 {
83 this.efTextBox1.Width = (base.Width - this.efLabel1.Width) - 2;
84 this.efTextBox1.Left = this.efLabel1.Width + 1;
85 }
86 }
87 else
88 {
89 this.efLabel1.Left = 0;
90 this.efLabel1.Top = 0;
91 this.efTextBox1.Left = 0;
92 if (this.m_bArrangeChanging)
93 {
94 base.Width = (this.efLabel1.Width > this.efTextBox1.Width) ? this.efLabel1.Width : this.efTextBox1.Width;
95 base.Height = (this.efLabel1.Height + this.efTextBox1.Height) + 2;
96 }
97 else
98 {
99 this.efLabel1.Height = (base.Height - this.efTextBox1.Height) - 2;
100 this.efTextBox1.Top = this.efLabel1.Height + 2;
101 }
102 this.efLabel1.Width = base.Width;
103 this.efTextBox1.Width = base.Width;
104 }
105 this.m_bArrangeChanging = false;
106 }
107
108 private void EFLabelText_Load(object sender, EventArgs e)
109 {
110 if (this.EFCname == null)
111 {
112 this.EFCname = base.Name;
113 }
114 }
115
116 private void EFLabelText_SizeChanged(object sender, EventArgs e)
117 {
118 this.ArrangeChange();
119 }
120
121 public bool EFSetFocus()
122 {
123 return this.efTextBox1.Focus();
124 }
125
126 private void efTextBox1_EFTextInvalid(object sender, EventArgs e)
127 {
128 this.errorProvider1.SetError(this, "格式输入错误,格式符为" + this.EFLeaveExpression + "!");
129 }
130
131 private void efTextBox1_KeyPress(object sender, KeyPressEventArgs e)
132 {
133 int keyChar = e.KeyChar;
134 if (this.item_type != ValueType.EFString)
135 {
136 if (this.item_type == ValueType.EFDecimal)
137 {
138 }
139 if (((keyChar < 0x30) || (keyChar > 0x39)) && ((keyChar != 0x2e) && (keyChar != 8)))
140 {
141 e.Handled = true;
142 }
143 }
144 }
145
146 private void efTextBox1_TextChanged(object sender, EventArgs e)
147 {
148 this.errorProvider1.SetError(this, "");
149 }
150
151 public Size getSize()
152 {
153 if (this.m_nArrange == ArrangeMode.horizontal)
154 {
155 return new Size((this.efTextBox1.Size.Width + this.efLabel1.Size.Width) + 2, base.Height);
156 }
157 return new Size(base.Width, this.efTextBox1.Size.Height + this.efLabel1.Size.Height);
158 }
159
160 protected virtual void OnDoEnterPress(object sender, EventArgs e)
161 {
162 if (this.DoEnterPress != null)
163 {
164 this.DoEnterPress(this, e);
165 }
166 }
167
168 protected override void OnPaint(PaintEventArgs e)
169 {
170 Graphics graphics = e.Graphics;
171 SizeF ef = graphics.MeasureString(this.efLabel1.Text, this.Font);
172 this.m_nLabelHorizontalLength = Convert.ToInt32(ef.Width) + 4;
173 ef = graphics.MeasureString(this.efTextBox1.Text, this.Font);
174 this.m_nTextBoxHorizontalLength = Convert.ToInt32(ef.Width);
175 if (this.m_bDynamicSize)
176 {
177 this.efTextBox1.Width = (this.m_nTextBoxHorizontalLength < 40) ? 40 : this.m_nTextBoxHorizontalLength;
178 base.Width = (this.efTextBox1.Width + this.efLabel1.Width) + 2;
179 }
180 if (this.m_nArrange == ArrangeMode.horizontal)
181 {
182 this.efLabel1.Width = this.m_nLabelHorizontalLength;
183 }
184 this.ArrangeChange();
185 }
186
187 public override void Refresh()
188 {
189 base.Refresh();
190 }
191
192 // Properties
193 [Category("EF属性"), Description("组成方式")]
194 public ArrangeMode EFArrange
195 {
196 get
197 {
198 return this.m_nArrange;
199 }
200 set
201 {
202 this.m_nArrange = value;
203 this.m_bArrangeChanging = true;
204 this.Refresh();
205 }
206 }
207
208 [Category("EF属性"), Description("中文")]
209 public string EFCname
210 {
211 get
212 {
213 return this.efLabel1.Text;
214 }
215 set
216 {
217 this.efLabel1.Text = value;
218 this.Refresh();
219 }
220 }
221
222 [Category("EF属性"),Description("英文")]
223 public string EFEname
224 {
225 get
226 {
227 return this.item_ename;
228 }
229 set
230 {
231 this.item_ename = value;
232 }
233 }
234
235 [Category("EF属性"),Description("输入文字")]
236 public string EFEnterText
237 {
238 get
239 {
240 return this.efTextBox1.Text;
241 }
242 set
243 {
244 this.efTextBox1.Text = value;
245 }
246 }
247
248 [ Category("EF属性"),Description("获得标签引用")]
249 public EFLabel EFLabelRef
250 {
251 get
252 {
253 return this.efLabel1;
254 }
255 }
256
257 [Category("EF属性"), Description("上一次正确的值")]
258 public string EFLastValidValue
259 {
260 get
261 {
262 return this.efTextBox1.EFLastValidValue;
263 }
264 }
265
266 [Category("EF属性"), Editor(typeof(RegexTypeEditor), typeof(UITypeEditor)), Description("焦点离开文本框时进行匹配检查的正则表达式")]
267 public string EFLeaveExpression
268 {
269 get
270 {
271 return this.efTextBox1.EFLeaveExpression;
272 }
273 set
274 {
275 this.efTextBox1.EFLeaveExpression = value;
276 }
277 }
278
279 [Category("EF属性"),Description("长度")]
280 public int EFLen
281 {
282 get
283 {
284 if (this.item_type == ValueType.EFDecimal)
285 {
286 return this.efTextBox1.MaxLength;
287 }
288 return this.efTextBox1.MaxLength;
289 }
290 set
291 {
292 int num;
293 if (value <= 0)
294 {
295 num = 1;
296 }
297 else
298 {
299 num = value;
300 }
301 if (this.item_type == ValueType.EFDecimal)
302 {
303 this.efTextBox1.MaxLength = num;
304 }
305 else
306 {
307 this.efTextBox1.MaxLength = num;
308 }
309 }
310 }
311
312 [Category("EF属性"), Description("获得文本引用")]
313 public EFTextBox EFTextRef
314 {
315 get
316 {
317 return this.efTextBox1;
318 }
319 }
320
321 [Category("EF属性"), Description("类型")]
322 public ValueType EFType
323 {
324 get
325 {
326 return this.item_type;
327 }
328 set
329 {
330 this.item_type = value;
331 if (value == ValueType.EFDateTime)
332 {
333 this.EFLen = 14;
334 }
335 }
336 }
337
338 [Category("EF属性"), Description("大写字母")]
339 public bool EFUpperCase
340 {
341 get
342 {
343 return (this.efTextBox1.CharacterCasing == CharacterCasing.Upper);
344 }
345 set
346 {
347 if (value)
348 {
349 this.efTextBox1.CharacterCasing = CharacterCasing.Upper;
350 }
351 else
352 {
353 this.efTextBox1.CharacterCasing = CharacterCasing.Normal;
354 }
355 }
356 }
357
358 // Nested Types
359 public delegate void EFEnterPressEventHandler(object sender, EventArgs e);
360 }
361 }
362