C# 如何将C中的文本框限制为只接收数字和(点或逗号),在“后”&引用;或&引用;仅允许2个数字字符
我正在尝试开发一个代码,用C#限制文本框只允许数字输入+逗号(“,”)或点(“.”)加上点或逗号后的两个数字 因此,通过这种方式可以看到可以输入的可能数字:C# 如何将C中的文本框限制为只接收数字和(点或逗号),在“后”&引用;或&引用;仅允许2个数字字符,c#,numbers,comma,restrict,C#,Numbers,Comma,Restrict,我正在尝试开发一个代码,用C#限制文本框只允许数字输入+逗号(“,”)或点(“.”)加上点或逗号后的两个数字 因此,通过这种方式可以看到可以输入的可能数字: 3213,04 = OK 3211,664 = Not 32.31 = OK 32.3214 = Not 334,,00 = Not 3247,.00 = Not 214.,00 = Not 32.. = Not 8465,0 = Ok 654.0 = Ok 明白我的目标吗? 我开发了下面的代码 private void txtValor
3213,04 = OK
3211,664 = Not
32.31 = OK
32.3214 = Not
334,,00 = Not
3247,.00 = Not
214.,00 = Not
32.. = Not
8465,0 = Ok
654.0 = Ok
明白我的目标吗?
我开发了下面的代码
private void txtValormetrocubico_KeyPress(object sender, KeyPressEventArgs e)
{
if (txtValormetrocubico.TextLength >= 0 && (e.KeyChar == (char)Keys.OemPeriod || e.KeyChar == (char)Keys.Oemcomma))
{
//tests
}
else
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.' && e.KeyChar != ',')
{
e.Handled = true;
}
// only allow one decimal point
if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
if (e.KeyChar == ',' && (sender as TextBox).Text.IndexOf(',') > -1)
{
e.Handled = true;
}
}
}
我想你需要像蒙面文本框控件这样的东西在这里你有一些参考资料 另一种方法是使用正则表达式,试试下面的代码! 我希望这有帮助。如果我能进一步帮助你,请告诉我 这是我写的辅助函数
private bool alreadyExist(string _text , ref char KeyChar)
{
if (_text.IndexOf('.')>-1)
{
KeyChar = '.';
return true;
}
if (_text.IndexOf(',') > -1)
{
KeyChar = ',';
return true;
}
return false;
}
这是您的按键事件处理程序
private void txtValormetrocubico_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.' && e.KeyChar != ',')
{
e.Handled = true;
}
//check if '.' , ',' pressed
char sepratorChar='s';
if (e.KeyChar == '.' || e.KeyChar == ',')
{
// check if it's in the beginning of text not accept
if (txtValormetrocubico.Text.Length == 0) e.Handled = true;
// check if it's in the beginning of text not accept
if (txtValormetrocubico.SelectionStart== 0 ) e.Handled = true;
// check if there is already exist a '.' , ','
if (alreadyExist(txtValormetrocubico.Text , ref sepratorChar)) e.Handled = true;
//check if '.' or ',' is in middle of a number and after it is not a number greater than 99
if (txtValormetrocubico.SelectionStart != txtValormetrocubico.Text.Length && e.Handled ==false)
{
// '.' or ',' is in the middle
string AfterDotString = txtValormetrocubico.Text.Substring(txtValormetrocubico.SelectionStart);
if (AfterDotString.Length> 2)
{
e.Handled = true;
}
}
}
//check if a number pressed
if (Char.IsDigit(e.KeyChar))
{
//check if a coma or dot exist
if (alreadyExist(txtValormetrocubico.Text ,ref sepratorChar))
{
int sepratorPosition = txtValormetrocubico.Text.IndexOf(sepratorChar);
string afterSepratorString = txtValormetrocubico.Text.Substring(sepratorPosition + 1 );
if (txtValormetrocubico.SelectionStart > sepratorPosition && afterSepratorString.Length >1)
{
e.Handled = true;
}
}
}
}
您可以创建一个通用函数,并在
keypress
事件中调用它。此代码是一个通用实例
validate_textBox
是一个通用功能
private void validate_textBox(TextBox _text, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.' && e.KeyChar != ',')
{
e.Handled = true;
}
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.' && e.KeyChar != ',')
{
e.Handled = true;
}
//check if '.' , ',' pressed
char sepratorChar = 's';
if (e.KeyChar == '.' || e.KeyChar == ',')
{
// check if it's in the beginning of text not accept
if (_text.Text.Length == 0) e.Handled = true;
// check if it's in the beginning of text not accept
if (_text.SelectionStart == 0) e.Handled = true;
// check if there is already exist a '.' , ','
if (alreadyExist(_text.Text, ref sepratorChar)) e.Handled = true;
//check if '.' or ',' is in middle of a number and after it is not a number greater than 99
if (_text.SelectionStart != _text.Text.Length && e.Handled == false)
{
// '.' or ',' is in the middle
string AfterDotString = _text.Text.Substring(_text.SelectionStart);
if (AfterDotString.Length > 2)
{
e.Handled = true;
}
}
}
//check if a number pressed
if (Char.IsDigit(e.KeyChar))
{
//check if a coma or dot exist
if (alreadyExist(_text.Text, ref sepratorChar))
{
int sepratorPosition = _text.Text.IndexOf(sepratorChar);
string afterSepratorString = _text.Text.Substring(sepratorPosition + 1);
if (_text.SelectionStart > sepratorPosition && afterSepratorString.Length > 1)
{
e.Handled = true;
}
}
}
}
然后,您可以为表单中的每个文本框调用类似此代码的函数
private void txtValormetrocubico_KeyPress(object sender, KeyPressEventArgs e)
{
validate_textBox(sender as TextBox, e);
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
validate_textBox(sender as TextBox, e);
}
谢谢,但我不喜欢戴面具的盒子。我更喜欢使用自定义代码,因为在字段填充之前,它不会显示“-”或$。。我已经全部工作了,只需要解决这个小问题就可以解决它了。不管怎样,谢谢你提供的信息。这个数字没有任何“.”或“,”好吗?123456879工作起来很有魅力,非常感谢,我会研究你做了什么+1给你。只有一个疑问,我有大约10个文本框,将在同一个windows窗体上使用此代码,将需要为每个按键事件创建复制和粘贴此大代码在代码中替换textname?或者我可以用另一种方法来引用这段代码(但我不知道如何解决textname问题)。谢谢again@felipeSalomao到目前为止,它可能与您无关,您可以简单地将该方法命名为“TextBoxValidate”之类的通用方法,并将其分配给所有10个文本框的按键事件,以便所有文本框调用相同的方法。下一步是使用给定给方法的sender变量,而不是txtValormetrocubico。因此,您将使用((文本框)sender)。它包含了适当的文本框,所以代码的其余部分应该可以在分配给它的任何文本框上正常工作。再次感谢你。在这段代码和我的整个项目中,它对我帮助太多了。我对C#还不太了解,但我学到的很多东西都是在stackoverflow上。谢谢你。