自定义控件只允许输入Decimal和int类型字符串
来源:互联网 发布:淘宝上的芝士零食知乎 编辑:程序博客网 时间:2024/04/29 23:49
自定义控件中只放了一个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 }}
0 0
- 自定义控件只允许输入Decimal和int类型字符串
- C#控件中只允许输入数字
- textbox控件中只允许输入整数
- TextBox控件只允许输入数字
- <input>标签 只输入 数字 int类型
- JS只允许输入数字和小数
- js只允许输入数字和小数点
- js只允许输入数字和小数点
- 只允许输入数字不允许输入字母和汉字和只允许粘贴数字
- 只允许输入数字
- 只允许输入数字
- 简单的,只允许输入数字的控件
- ASP.Net TextBox控件只允许输入数字
- ASP.Net TextBox控件只允许输入数字
- ASP.Net TextBox控件只允许输入数字(转)
- ASP.Net TextBox控件只允许输入数字(转)
- ASP.Net TextBox控件只允许输入数字(转)
- ASP.Net TextBox控件只允许输入数字
- html link rel='stylesheet' 什么意思?
- Oracle RAC 常用维护工具和命令
- 面试10大算法汇总+常见题目解答
- PHP preferences
- 获取程序自身签名公钥+程序自检测签名
- 自定义控件只允许输入Decimal和int类型字符串
- STL泛型编程-deque双端队列容器
- 带有查询和排序功能的jquery分页插件——DataTables
- Eclipse配置Git
- htop--linux下最强大的进程监视器
- First Missing Positive 找第一个没在array中出现的正数@LeetCode
- yii全局函数的使用
- Java集合之ArrayList
- NSString什么时候用copy,什么时候用strong