How to: Create a Numeric Text Box
来源:互联网 发布:美国囧哥淘宝店 编辑:程序博客网 时间:2024/04/29 15:56
public class NumericTextBox : TextBox
{
bool allowSpace = false;
// Restricts the entry of characters to digits (including hex), the negative sign,
// the decimal point, and editing keystrokes (backspace).
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
NumberFormatInfo numberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
string groupSeparator = numberFormatInfo.NumberGroupSeparator;
string negativeSign = numberFormatInfo.NegativeSign;
string keyInput = e.KeyChar.ToString();
if (Char.IsDigit(e.KeyChar))
{
// Digits are OK
}
else if (keyInput.Equals(decimalSeparator) || keyInput.Equals(groupSeparator) ||
keyInput.Equals(negativeSign))
{
// Decimal separator is OK
}
else if (e.KeyChar == '/b')
{
// Backspace key is OK
}
// else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0)
// {
// // Let the edit control handle control and alt key combinations
// }
else if (this.allowSpace && e.KeyChar == ' ')
{
}
else
{
// Consume this invalid key and beep
e.Handled = true;
// MessageBeep();
}
}
public int IntValue
{
get
{
return Int32.Parse(this.Text);
}
}
public decimal DecimalValue
{
get
{
return Decimal.Parse(this.Text);
}
}
public bool AllowSpace
{
set
{
this.allowSpace = value;
}
get
{
return this.allowSpace;
}
}
}
调用代码:
// Create an instance of NumericTextBox.
NumericTextBox numericTextBox1 = new NumericTextBox();
numericTextBox1.Parent = this;
- How to: Create a Numeric Text Box
- How To Create a Simple Magazine App with Core Text
- How To Create a Simple Magazine App with Core Text
- how-to-create-a-simple-magazine-app-with-core-text
- How to Create a Sublime Text 2 Plugin
- how to create list box example
- How to Create a Customer
- how to create a Makefile
- How to generate a random alpha-numeric string in java
- How to Create a REST Protocol
- How to create a user in PostgreSQL
- How to create a javascript class~
- How to create a test plan?
- how to create a sln file
- How to create a minimal master page
- How to create/display a modal window?
- How to create a new routine
- How to create a PDF file, cheaply?
- 堆栈的简单lua实现
- java 孙鑫 第六课(第三部分)TreeSet介绍
- java 孙鑫 第六课(第四部分)HashMap和TreeMap类介绍
- DOS如何配置内存(config.sys)
- 用JS访问操作iframe框架里的dom
- How to: Create a Numeric Text Box
- 使用 iframe 获取网页片段的一个好处
- 纯DOS下内存的管理—实模式下访问4GB内存
- 装载iframe 子页面,自适应高度
- J2EE API 5.0 html、chm 版下载
- Java编程那些事儿6——计算机软件基本概念
- Java编程那些事儿7——进制的概念
- Java编程那些事儿8——计算机内部的数据表达
- Jquery实现div模拟Select控件