积极答复者
webcast里的那个做只能输入数字的textbox是不行的吧

问题
答案
-
Hi,
能否提供些上下文以及具体出错的现象呢? 以便做重现。
以下代码可作为参考,控制输入的只能是数值类型。
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 (this.allowSpace && e.KeyChar == ' ') { } else { e.Handled = true; } }
Thanks.
Microsoft Online Community Support Please remember to mark the replies as answers if they help and unmark them if they provide no help.- 已标记为答案 Jiong ShiMVP, Moderator 2011年3月16日 7:13
全部回复
-
Hi,
能否提供些上下文以及具体出错的现象呢? 以便做重现。
以下代码可作为参考,控制输入的只能是数值类型。
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 (this.allowSpace && e.KeyChar == ' ') { } else { e.Handled = true; } }
Thanks.
Microsoft Online Community Support Please remember to mark the replies as answers if they help and unmark them if they provide no help.- 已标记为答案 Jiong ShiMVP, Moderator 2011年3月16日 7:13
-
- 已标记为答案 微软中文技术论坛Moderator 2009年6月15日 6:05
- 取消答案标记 微软中文技术论坛Moderator 2009年6月15日 6:05