textBox检验是否是数字? 我是用的VB 我想请问textbox中只能输入数字 怎么写函数 虚心请教 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 c#可以么?订阅TextBox的KeyPress事件Private void textBox_KeyPress(object sender,KeyPressEventArgs e){ if((e.KeyChar<48 || e.KeyChar>57) && e.KeyChar!=8)//ASCII 48-57是数字,8是退格 e.Handled=true;} int number;bool bl=int.tryparse(strInput,out number);//也可以使用uint.parseif(!bl){number=1;}--Regex: ^\d+$ private bool IsHaveInteger(TextBox tb){ bool isHave; if (tb.Trim().Length == 0) { isHave = false; } else { if (System.Text.RegularExpressions.Regex.IsMatch(tb.Trim().ToString(), @"^[0-9]+$")) { isHave = true; } else { isHave = false; } } return isHave;} 说一个思路,不过是C#代码 private String perTest = String.Empty; // 记录合理的上一次text private void mTextBox_TextChanged(object sender, EventArgs e) { String text = mTextBox.Text; if (String.Empty != text) { int curIndex = mTextBox.SelectionStart; int num; if (!int.TryParse(text, out num)) { mTextBox.Text = preLabelSize; mTextBox.SelectionStart = curIndex - 1; } else { preLabelSize = mTextBox.Text; } } }这样就实现你说的效果,并且光标停在上次合理的位置。 perTest = mTextBox.Text; Sorry! 看你是验证整数还是小数 public bool CheckDouble(char a) { if((a<48 || a>57)&&(a!=46)&&(a!=8)) { return false; } return true; } public bool CheckInt(char a) { if ((a < 48 || a > 57) &&(a != 8)) { return false; } return true; }然后再写textbox的KeyPress事件 private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (!CheckInt(e.KeyChar)) { e.Handled = true; } } try{int.parse(textbox.text);}catch(Exception ex){MessageBox.Show(ex.Message);} 问个DataGridView复制行的问题 请问接口一般用在什么地方呢 biztalk map 关于委托的疑问 关于OleDbDataReader读取ACCESS顺序错乱的问题 datawindow .net 如何用鼠标点击后高亮选中一行 GDI+ 向一个矩形区域内画文字,如何根据该区域的大小得到文字的字体大小 C#把txt中的前5行数据读出,并显示出来 Windows Application 客戶端必須要架.net嗎?? 有沒有什麼辦法.....這樣也太.... “异常”是什么啊?我看不懂! c#中如何连接数据库的两个表 关于StreamReader,如何正常读取未知编码格式的文本文件?
订阅TextBox的KeyPress事件Private void textBox_KeyPress(object sender,KeyPressEventArgs e)
{
if((e.KeyChar<48 || e.KeyChar>57) && e.KeyChar!=8)//ASCII 48-57是数字,8是退格
e.Handled=true;
}
int number;
bool bl=int.tryparse(strInput,out number);//也可以使用uint.parse
if(!bl)
{
number=1;
}
--Regex: ^\d+$
{
bool isHave;
if (tb.Trim().Length == 0)
{
isHave = false;
}
else
{
if (System.Text.RegularExpressions.Regex.IsMatch(tb.Trim().ToString(), @"^[0-9]+$"))
{
isHave = true;
}
else
{
isHave = false;
}
}
return isHave;
}
private void mTextBox_TextChanged(object sender, EventArgs e)
{
String text = mTextBox.Text;
if (String.Empty != text)
{
int curIndex = mTextBox.SelectionStart;
int num;
if (!int.TryParse(text, out num))
{
mTextBox.Text = preLabelSize;
mTextBox.SelectionStart = curIndex - 1;
}
else
{
preLabelSize = mTextBox.Text;
}
}
}这样就实现你说的效果,并且光标停在上次合理的位置。
Sorry!
public bool CheckDouble(char a)
{
if((a<48 || a>57)&&(a!=46)&&(a!=8))
{
return false;
}
return true;
}
public bool CheckInt(char a)
{
if ((a < 48 || a > 57) &&(a != 8))
{
return false;
}
return true;
}然后再写textbox的KeyPress事件
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!CheckInt(e.KeyChar))
{
e.Handled = true;
}
}
{
int.parse(textbox.text);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}