自定义控件只允许输入Decimal和int类型字符串
2016-01-28 00:00
316 查看
自定义控件中只放了一个TextBox控件,并在TextBox下利用自定义控件的Paint画了一条线,然后给TextBox做了3个自定义属性,分别是TextBoxText属性,方便取值;IntBool属性,是否只输入正整数;inputDecimal是否只能输入小数。
以下是整个自定义控件的源代码
以下是整个自定义控件的源代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.Text.RegularExpressions; namespace GCIMS.CommonCtrl { public partial class ctrlUnderlineTextBox : UserControl { #region 属性 /// <summary> /// 控件的文本 /// </summary> [Description("文本值"), Browsable(true), Category("自定义属性")] public string TextBoxText { get { return textBox.Text; } set { textBox.Text = value; } } private bool intBool; /// <summary> /// 只输入正整数 /// </summary> [Description("只输入正整数"), Browsable(true), Category("自定义属性"), DefaultValue(false)] public bool IntBool { get { return intBool; } set { intBool = value; if (intBool) { textBox.KeyPress += new KeyPressEventHandler(IntBool_KeyPress); } } } private bool inputDecimal; /// <summary> /// 只输入小数 /// </summary> [Description("只输入小数"), Browsable(true), Category("自定义属性"), DefaultValue(false)] public bool InputDecimal { get { return inputDecimal; } set { inputDecimal = value; if (inputDecimal) { textBox.KeyPress += new KeyPressEventHandler(InputDecimal_KeyPress); } } } #endregion #region 构造函数 public ctrlUnderlineTextBox() { InitializeComponent(); } #endregion #region 事件-ctrlUnderlineTextBox_KeyPress private void IntBool_KeyPress(object sender, KeyPressEventArgs e) { if ((int)e.KeyChar >= 48 && (int)e.KeyChar <= 57 && textBox.Text.Length < 18 || (int)e.KeyChar == 8) //只能输入0-9数字和BackSpace { e.Handled = false; } else { e.Handled = true; } } #endregion #region 事件-InputDecimal_KeyPress private void InputDecimal_KeyPress(object sender, KeyPressEventArgs e) { //搜索字符串中的'.'字符 string textBoxStr = textBox.Text; Regex rg = new Regex("."); MatchCollection mc = rg.Matches(textBoxStr); int textBoxCount = mc.Count; //允许输入数字,小数点,退格键,不允许输入大于18为的数字,不允许输入两个小数点 if ((int)e.KeyChar >= 48 && (int)e.KeyChar <= 57 && textBox.Text.Length < 18 || (int)e.KeyChar == 8 || e.KeyChar == '.' && textBoxCount <= 1) //只能输入0-9数字和BackSpace { e.Handled = false; } else { e.Handled = true; } } #endregion #region 事件-CtrlUnderlineTextBox_Paint private void CtrlUnderlineTextBox_Paint(object sender, PaintEventArgs e) { Graphics g = Graphics.FromHwnd(this.Handle); System.Drawing.Pen pen = new Pen(Color.Black); PointF point1 = new PointF(textBox.Location.X, textBox.Location.Y + textBox.Height); PointF point2 = new PointF(textBox.Location.X + textBox.Width, textBox.Location.Y + textBox.Height); g.DrawLine(pen, point1, point2); } #endregion } }
相关文章推荐
- WCF双工服务端回调客户端
- 谈谈软件的高扩展性、高可用性、高可维护性-------针对网银接入项目的思考
- Spring事务配置的五种方式
- 【grunt整合版】30分钟学会使用grunt打包前端代码
- maven + grunt + tomcat + cmd + sublime
- md5等hash算法加密解密问题
- XML之JDOM解析
- 一些小知识点
- easyUI 加载层
- .net 获取当前DLL所在目录
- mysql 替换某个字段中的某个字符
- Java之注解初探
- Struts2 result type(结果类型)
- 设计模式之Factory
- Android中Parcelable接口用法总结
- 数据的存取
- 字符串的增删改查
- kafka参数配置详解
- Cent OS 7 安装 mysql-5.7
- 个人社保缴纳接口 保公积金缴费数据实现