[英]Regex for a TextBox
I am trying to remove the last entered letter in a texbox. 我正在尝试删除texbox中最后输入的字母。 I am using a Regex to determine if the entry is valid.
我正在使用正则表达式来确定条目是否有效。 However, the code I have only checks the first character, and stops after that.
但是,我拥有的代码仅检查第一个字符,然后停止。 Not sure what I am doing wrong.
不知道我在做什么错。
I want the user to be able to input as many numbers as they want (including negative numbers). 我希望用户能够输入所需数量的数字(包括负数)。
Here is my code: 这是我的代码:
private void textBox_Gen_Offset_TextChanged(object sender, EventArgs e)
{
Regex pattern = new Regex("[-+]?[0-9]*");
if (pattern.IsMatch(textBox_Gen_Offset.Text))
{
UpdateTotal();
}
else
{
if (textBox_Gen_Offset.Text.Length > 0)
{
textBox_Gen_Offset.Text = textBox_Gen_Offset.Text.Substring(0, textBox_Gen_Offset.Text.Length - 1);
}
}
}
I'm not an expert on regex, but you could check if entered data is numeric by trying to parse is as an int or a double (for integers or floating point numbers respectively). 我不是正则表达式方面的专家,但是您可以通过尝试将输入数据解析为int或double(分别用于整数或浮点数)来检查输入的数据是否为数字。
int intInTheBox;
if (int.TryParse(textBox_Gen_Offset.Text, out intInTheBox))
{
//Do stuff if it's an int
}
double doubleInTheBox;
if (double.TryParse(textBox_Gen_Offset.Text, out doubleInTheBox))
{
//Do stuff if it's a double
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.