• 全部
...

C#textbox控件中只允许输入整数的问题

liushaohuayunjie 2010-12-22 02:20:39
我的问题是在textbox控件中只允许输入整数,点击添加按钮后 将信息添加到listbox控件中。但现在让我头疼的是不知道如何限制只允许输入整数。望前辈解答,谢谢
...全文
给本帖投票
1067 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
rabbitlzx 2010-12-22
  • 打赏
  • 举报
回复
各位,用得着搞这么复杂么,一个MaskedTextBox就搞定了,非要弄什么正则表达式??
_三皮_ 2010-12-22
  • 打赏
  • 举报
回复
private bool nonNumberEntered = false;
private void txtCYC_M_KeyDown(object sender, KeyEventArgs e)
{
// Initialize the flag to false.
nonNumberEntered = false;

// Determine whether the keystroke is a number from the top of the keyboard.
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
// Determine whether the keystroke is a number from the keypad.
if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
{
// Determine whether the keystroke is a backspace.
if (e.KeyCode != Keys.Back)
{
// A non-numerical keystroke was pressed.
// Set the flag to true and evaluate in KeyPress event.
nonNumberEntered = true;
}
}
}
//If shift key was pressed, it's not a number.
if (Control.ModifierKeys == Keys.Shift)
{
nonNumberEntered = true;
}

}

private void txtCYC_M_KeyPress(object sender, KeyPressEventArgs e)
{
// Check for the flag being set in the KeyDown event.
if (nonNumberEntered == true)
{
// Stop the character from being entered into the control since it is non-numerical.
e.Handled = true;
}

}
wuyq11 2010-12-22
  • 打赏
  • 举报
回复
<input onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">

Regex.IsMatch(strIn, @"^(0|[1-9]\d*)\.(\d\d)$");


thisiscandy 2010-12-22
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20080626/11/a17ab62f-aced-4774-8320-a97f6ebe5c8a.html
是winform的吧,这个链接中的比较全。
rabbitlzx 2010-12-22
  • 打赏
  • 举报
回复
用MaskedTextBox控件
sky_too_sky 2010-12-22
  • 打赏
  • 举报
回复
js
正则表达式
卡索瓦 2010-12-22
  • 打赏
  • 举报
回复
http://blog.csdn.net/zh275589797/archive/2010/12/22/6091771.aspx
luxiaodongly 2010-12-22
  • 打赏
  • 举报
回复
重新 定义一个控件:public class NumberTextBox:System.Windows.Forms.TextBox

然后重写函数WndProc
截获WM_CHAR:消息 对这个消息 进行处理.

111,099

社区成员

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

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

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

手机看
关注公众号

关注公众号

客服 返回
顶部