C# 按键事件小数点后的限制数字
我使用以下代码只从用户处获取数字和一个小数点,这在按键事件中对我来说很好:C# 按键事件小数点后的限制数字,c#,numbers,decimal,limit,digit,C#,Numbers,Decimal,Limit,Digit,我使用以下代码只从用户处获取数字和一个小数点,这在按键事件中对我来说很好: if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.') { e.Handled = true; } if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1) { e.Ha
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
}
if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
现在我想限制小数点后的数字/位数,即35.25468,这意味着小数点后只需要6个数字/位数
更新我 在按键事件和/或验证事件上,计算小数点后的字符数。按此键时,将其抑制。在验证时,删除额外的小数位。请确保您从NumberFormatInfo获取小数点字符,并不是所有区域性都使用“.”,即在法国,它们的小数点实际上是一个逗号
TextBox textBox = (TextBox) sender;
if (textBox.Text.IndexOf('.') > -1 &&
textBox.Text.Substring(textBox.Text.IndexOf('.')).Length >=3)
{
e.Handled = true;
}
注意,子字符串将包含“.”,因此在按键时,复选框为
=3
,格式化字符串并将textBox.Text
设置为格式化字符串
TextBox.Text = String.Format("{0:N3"}", textBox.Text)
这种特殊格式在第三个小数点处截断数字
private void price_tb_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(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 (!char.IsControl(e.KeyChar))
{
TextBox textBox = (TextBox)sender;
if (textBox.Text.IndexOf('.') > -1 &&
textBox.Text.Substring(textBox.Text.IndexOf('.')).Length >= 3)
{
e.Handled = true;
}
}
}
此代码将帮助您。它只需要一个小数位和一个小数位后的两位数字,您可以相应地更改它。I had
textBox.SelectionLength==0
允许修改所选文本:
private void price_tb_KeyPress(object sender, KeyPressEventArgs e) {
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.') {
e.Handled = true;
}
TextBox textBox = (TextBox)sender;
// only allow one decimal point
if (e.KeyChar == '.' && textBox.Text.IndexOf('.') > -1) {
e.Handled = true;
}
if (!char.IsControl(e.KeyChar) && textBox.SelectionLength == 0) {
if (textBox.Text.IndexOf('.') > -1 && textBox.Text.Substring(textBox.Text.IndexOf('.')).Length >= 3) {
e.Handled = true;
}
}
}
我对这两个FM的回答都有一个问题,就是当你输入了一个小数位和两个小数位时,你不能编辑文本 此代码也接受负数
private void TextBoxAmount_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar))
{
// OK, but not more than 2 after the [.]
if (((TextBox)sender).Text.Contains('.'))
{
if (((TextBox)sender).Text.IndexOf('.') + 2 < ((TextBox)sender).Text.Length)
{
if (((TextBox)sender).SelectionStart > ((TextBox)sender).Text.IndexOf('.'))
{
e.Handled = true;
}
}
}
}
else if (char.IsControl(e.KeyChar))
{
// Always OK
}
else if (e.KeyChar == '.' && !((TextBox)sender).Text.Contains('.'))
{
// First [.] == OK
}
else if (e.KeyChar == '-' && !((TextBox)sender).Text.Contains('-'))
{
// First [-] == OK
}
else
{
e.Handled = true;
}
}
private void TextBoxAmount_KeyUp(object sender, KeyEventArgs e)
{
if (((TextBox)sender).Text.Contains('-'))
{
((TextBox)sender).Text = $"-{((TextBox)sender).Text.Replace("-", string.empty)}";
}
}
private void TextBoxAmount\u按键(对象发送者,按键事件参数e)
{
if(字符IsDigit(e.KeyChar))
{
//可以,但在[.]之后不超过2
如果(((文本框)发件人).Text.Contains('.'))
{
如果(((文本框)发送方).Text.IndexOf('.')+2<((文本框)发送方).Text.Length)
{
如果(((文本框)发件人)。选择开始>((文本框)发件人)。Text.IndexOf('.'))
{
e、 已处理=正确;
}
}
}
}
else if(char.IsControl(e.KeyChar))
{
//总是好的
}
如果(e.KeyChar='.&&!((文本框)sender).Text.Contains('.'),则为else
{
//第一[.]==正常
}
else if(e.KeyChar=='-'&&&!((文本框)sender.Text.Contains('-'))
{
//第一个[-]==正常
}
其他的
{
e、 已处理=正确;
}
}
私有void TextBoxAmount\u KeyUp(对象发送方,KeyEventArgs e)
{
if(((文本框)sender).Text.Contains('-'))
{
((TextBox)sender.Text=$”-{((TextBox)sender.Text.Replace(“-”,string.empty)}”;
}
}
请确保您从NumberFormatInfo获取小数点字符,并非所有文化都使用“.”,即在法国,他们的小数点实际上是一个逗号。如果可以这样做,我强烈反对,因为如果用户试图编辑字段,这会让他们非常沮丧。它还使代码相当复杂。如果您在用户退出字段或提交表单后进行验证,那么您的情况会更好。使用内置的验证工具。这就是它的目的。