[.net自定义控件]TextBox控件重写 之NumTextBox
来源:百度文库 编辑:神马文学网 时间:2024/05/24 01:21:52
TextBox控件重写 之NumTextBox,主要实现的功能是,只允许输入数字,或String,Numeric,Currency,Decimal,Float,Double,Short,Int,Long
等加了一个属性InputType可以设置其,还可以自己进一步扩展,因为是开源的。
public enum NumTextBoxType
{
String,//是这个的时候,什么都不处理,跟正常TextBox一样
Numeric,//只要是数字就行
Currency,
Decimal,
Float,
Double,
Short,
Int,
Long
}
主要代码:
view plaincopy to clipboardprint?- using System;
- using System.Windows.Forms;
- using System.ComponentModel;
- using System.Text.RegularExpressions;
- namespace YongFa365.Controls.NumTextBox
- {
- public class NumTextBox : TextBox
- {
- private NumTextBoxType inputType = NumTextBoxType.Numeric;
- public enum NumTextBoxType
- {
- String,//是这个的时候,什么都不处理,跟正常TextBox一样
- Numeric,//只要是数字就行
- Currency,
- Decimal,
- Float,
- Double,
- Short,
- Int,
- Long
- }
- public NumTextBox()
- {
- this.ContextMenu = new ContextMenu();
- }
- [
- Category("专用设置"),
- DefaultValue(NumTextBoxType.Numeric),
- Description("设置允许类型:\nString跟普通TextBox功能一样\nNumeric只要是数字就可以")
- ]
- public NumTextBoxType InputType
- {
- get { return inputType; }
- set { inputType = value; }
- }
- public override string Text
- {
- get { return base.Text; }
- set
- {
- if (IsValid(value, true))
- {
- base.Text = value;
- }
- }
- }
- private bool IsValid(string val, bool use)
- {
- bool ret = true;
- if (string.IsNullOrEmpty(val))
- {
- return ret;
- }
- if (use)
- {
- if (val.Equals("-") && inputType != NumTextBoxType.Numeric)
- {
- return ret;
- }
- }
- try
- {
- switch (inputType)
- {
- case NumTextBoxType.String:
- break;
- case NumTextBoxType.Numeric:
- if (!Regex.IsMatch(val, @"^\d*$"))
- {
- ret = false;
- }
- break;
- case NumTextBoxType.Currency:
- decimal dec = decimal.Parse(val);
- int pos = val.IndexOf(".");
- if (pos != -1)
- {
- ret = val.Substring(pos).Length <= 3;
- }
- break;
- case NumTextBoxType.Float:
- float flt = float.Parse(val);
- break;
- case NumTextBoxType.Double:
- double dbl = double.Parse(val);
- break;
- case NumTextBoxType.Decimal:
- decimal dec2 = decimal.Parse(val);
- break;
- case NumTextBoxType.Short:
- short s = short.Parse(val);
- break;
- case NumTextBoxType.Int:
- int i = int.Parse(val);
- break;
- case NumTextBoxType.Long:
- long l = long.Parse(val);
- break;
- default:
- throw new ApplicationException();
- }
- }
- catch
- {
- ret = false;
- }
- return ret;
- }
- protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
- {
- if (keyData == (Keys)Shortcut.CtrlV || keyData == (Keys)Shortcut.ShiftIns)
- {
- IDataObject iData = Clipboard.GetDataObject();
- string newText;
- newText = base.Text.Substring(0, base.SelectionStart)
- + (string)iData.GetData(DataFormats.Text)
- + base.Text.Substring(base.SelectionStart + base.SelectionLength);
- if (!IsValid(newText, true))
- {
- return true;
- }
- }
- return base.ProcessCmdKey(ref msg, keyData);
- }
- protected override void OnLeave(EventArgs e)
- {
- if (!(inputType == NumTextBoxType.Numeric || inputType == NumTextBoxType.String))
- {
- if (base.Text != "")
- {
- if (!IsValid(base.Text, false))
- {
- base.Text = "";
- }
- else if (Double.Parse(base.Text) == 0)
- {
- base.Text = "0";
- }
- }
- }
- base.OnLeave(e);
- }
- protected override void OnKeyPress(KeyPressEventArgs e)
- {
- if (inputType != NumTextBoxType.String)
- {
- char c = e.KeyChar;
- if (!Char.IsControl(c))
- {
- if (c.ToString() == " ")
- {
- e.Handled = true;
- return;
- }
- string newText = base.Text.Substring(0, base.SelectionStart)
- + c.ToString() + base.Text.Substring(base.SelectionStart + base.SelectionLength);
- if (!IsValid(newText, true))
- {
- e.Handled = true;
- }
- }
- }
- base.OnKeyPress(e);
- }
- }
- }
[.net自定义控件]TextBox控件重写 之NumTextBox
VB.NET中的TextBox控件详解2
自定义控件
Asp.net 2.0 自定义控件开发[创建自定义右键PopupMenu控件]
Asp.net 2.0 自定义控件开发[创建自定义右键PopupMenu控件]
ASP.NET 2.0服务器控件开发之控件样式
ASP.NET 2.0服务器控件之复合控件概述
ASP.NET 2.0服务器控件之复合控件事件
使用自定义函数对 ASP.NET 服务器控件进行验证
symbian自定义控件
实用的TextBox控件的Undo属性
ASP.NET2.0服务器控件之自定义状态管理
vb.net控件
创建自定义 AJAX 客户端控件
Silverlight 自定义控件模板管理
net中如何用findcontrol获得GridView自定义控件的值
自动适应输入内容高度的TextBox控件
适用于 Visual Basic 6.0 用户的 TextBox 控件
定制VB.NET控件编程之拦截击键动作
.NET控件名称缩写一览表
vb.net的ListBox控件
vb.net的Combobox控件
轻松调整VB.NET控件
善用 .NET 的 PropertyGrid 控件