3

我正在使用以下代码只取用户的数字和一个小数点,这对我来说在 KeyPress Event 上工作正常:

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 个数字/数字。

告诉我 !

4

6 回答 6

5
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;
        }

        }

    }

此代码将为您提供帮助。它只需要一位小数和一位小数后两位数,您可以相应地更改它。

于 2011-09-21T01:56:05.710 回答
0

在按键事件和/或验证事件上,计算小数点后的字符数。在按键时,抑制它。在验证时,删除多余的小数位。确保您从 NumberFormatInfo 获取小数点字符,并非所有文化都使用“.”,即。在法国,他们的小数点实际上是一个逗号

于 2011-08-31T15:15:31.000 回答
0

您可以像这样添加额外的检查

TextBox textBox = (TextBox) sender;

if (textBox.Text.IndexOf('.') > -1 &&
         textBox.Text.Substring(textBox.Text.IndexOf('.')).Length >=3)
{
    e.Handled = true;
}

请注意,子字符串将包括“。” 因此支票是>=3.

于 2011-08-31T15:20:08.950 回答
0

在按键上,格式化字符串并将 设置textBox.Text为格式化的字符串。

TextBox.Text = String.Format("{0:N3"}", textBox.Text)

这种特殊的格式会截断小数点后第 3 位的数字。

于 2011-08-31T15:23:27.570 回答
0

我必须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;
        }
    }
}
于 2012-10-22T15:25:25.903 回答
0

我对 Both 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)}";
        }
    }
于 2020-07-07T08:05:18.307 回答