程序开发中,经常用到一个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 

转载于:https://www.cnblogs.com/jiangshaofen/archive/2009/10/10/1580405.html

整合TextBox与Label 创建新控件--EFLabelText相关推荐

  1. firefox扩展开发(二):用XUL创建窗口控件

    firefox扩展开发(二):用XUL创建窗口控件 2008-06-11 16:57 1.创建一个简单的窗口 <?xml version="1.0"?> <?xm ...

  2. [原]动态创建Web控件制做计算器

    最近参加了Web基础开发的培训,收获不少,做了一个练习,在后台动态创建控件制作了一个简单功能的计算器.程序中控件创建好以后,往往不能放在想要的位置,前台的布局非常麻烦,我用Table.TableRow ...

  3. 利用css对shiny页面优化及利用htmlwidgets包创建HTML控件

    内容来源:2017年5月20日,乐逗游戏高级数据分析师在"第十届中国R会议软件工具专场"进行<HTTPS最佳安全实践>演讲分享.IT大咖说作为独家视频合作方,经主办方和 ...

  4. 一个Demo学会用Android兼容包新控件

    2019独角兽企业重金招聘Python工程师标准>>> 前言 伟大的Google为Android推出了一系列的兼容包,最新的就是Design Support Library了,这里我 ...

  5. FindChildControl与FindComponent(动态创建的控件要通过Owner.FindComponent去找该控件)

    前两天编码遇到了要使用FindChildControl方法获取指定名称的TSpeedButton按钮,结果折腾了半天就是没得结果(基础不扎实,呵呵),于是赶紧搜索了下,补习关于这两个方法的用法. TW ...

  6. UWP开发随笔——UWP新控件!AutoSuggestBox!

    UWP开发随笔--UWP新控件!AutoSuggestBox! 原文:UWP开发随笔--UWP新控件!AutoSuggestBox! 摘要 要开发一款优秀的application,控件肯定是必不可少的 ...

  7. android Snackbar新控件解析

    Dialog和Toast,我们在日常的开发中一定非常熟悉,常常被用来作为Android应用内提示性信息的两种展示方式.然而Google在Design包中又提供了一种新的选择,那就是Snackbar.今 ...

  8. 【079】用代码来创建 Android 控件

    一般来说我们在创建控件的时候都是在 XML 文件中完成的, 实施起来还是蛮方便的, 而且修改起来也可以很快的看见效果, 但是有一个很大的劣势就是没办法动态的创建控件, 举个例子, 例如我从数据库中取出 ...

  9. 【Android】Anroid5.0+新控件---酷炫标题栏的简单学习

    Android5.0+推出的新控件感觉特别酷,最近想模仿大神做个看图App出来,所以先把这些新控件用熟悉了. 新控件的介绍.使用等等网上相应的文章已经特别多了,题主也没那能力去写篇详解出来,本篇随笔记 ...

最新文章

  1. Redis Server Memory Optimization
  2. Java Se:自定义ClassLoader
  3. spring 整合junit进行测试
  4. Hessian 初探
  5. Java并发编程—常见面试题
  6. Python+Opencv识别两张相似图片
  7. 【剑指offer】_18 数据流中的中位数
  8. synthesize和dynamic
  9. Atitit orm优缺点 Hinaernate mybatis 区别。attilax总结
  10. Vb.net遍历一个窗口中的所有某类对象 (窗体中的控件) 的方法
  11. stm32+lcd显示汉字之GBK编码
  12. 未来计算机体系结构探索,未来计算机体系结构将是什么样的发展趋势
  13. 新手小白做短视频自媒体,入门级教程分享,抓紧收藏
  14. OpenCV_用形态学运算变换图像
  15. 【转载】校园网络客户端连网常见问题
  16. .Net面试经验总结
  17. 前端eslint+prettier+lint-staged配置
  18. Docker容器之compose容器集群的快速编排
  19. 停不下的脚步:IT高管人士的工作实录
  20. 小猿圈python_小猿圈Python配置gRPC环境

热门文章

  1. Java常用的几个Json库
  2. Windows10/Servers2016应用商店恢复/安装
  3. linux 7 %3e命令,Linux操作系统常用基础命令
  4. echarts源码打包_Echarts源码阅读指南
  5. hql删除mysql语句_hibernate hql删除异常
  6. python鼠标位置_用python3 返回鼠标位置的实现方法(带界面)
  7. python 类 字典_python基础类型—字典
  8. IP组播之组管理协议IGMP
  9. Wannafly交流赛1: C. 腰带图(瞎搞)
  10. bzoj 1618: [Usaco2008 Nov]Buying Hay 购买干草(完全背包)