C#中textbox只接受数字的问题

dggen1 2008-10-25 10:43:52
不是有个属性Numeric吗?为什么用不了?
我知道可以用keypress的方法判断?但是为什么Numeric不行?MSDN上有这个属性啊!
...全文
361 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
slimfeng 2008-10-26
  • 打赏
  • 举报
回复
WinForm中的TextBox控件没有Numeric属性,需要创建新控件类,加上IsNumeric方法(采用正则方法判断)
当然你可以采用你知道的KeyPress方法去处理
firefoxxx 2008-10-26
  • 打赏
  • 举报
回复
Winform开发中的TextBox类和Web中的不是同一个类

Winform中可以用Regex来验证 @^\d*$
空白是我 2008-10-26
  • 打赏
  • 举报
回复

static bool IsNumeric(string str)
{
System.Text.RegularExpressions.Regex reg1
= new System.Text.RegularExpressions.Regex(@"^[-]?d+[.]?d*$");
return reg1.IsMatch(str);
}

以上是用正则表达式来判断是否数字!
goldxinx 2008-10-25
  • 打赏
  • 举报
回复
太多方法 做这个事
IMAGSE 2008-10-25
  • 打赏
  • 举报
回复
可以用正则表达式啊~
王集鹄 2008-10-25
  • 打赏
  • 举报
回复
TextBox.Numeric 属性

获取一个值,该值指示输入是否必须全部是数值。默认为 false。

命名空间:System.Web.UI.MobileControls

System.Web.Mobile 命名空间包含生成 ASP.NET 移动 Web 应用程序所需的核心功能,包括身份验证和错误处理。

---------

这个TextBox和你说的TextBox不是同一个命名空间,是不同的类。

using System.Runtime.InteropServices;

[DllImport("User32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("User32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
public const int GWL_STYLE = -16;
public const int ES_NUMBER = 0x2000;
private void Form1_Load(object sender, EventArgs e)
{
SetWindowLong(textBox1.Handle, GWL_STYLE,
GetWindowLong(textBox1.Handle, GWL_STYLE) | ES_NUMBER);
}
victory870826 2008-10-25
  • 打赏
  • 举报
回复
欲知!!

111,119

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧