限制按键事件中小数点后的数字

发布于 2024-12-02 16:54:32 字数 382 浏览 1 评论 0原文

我使用以下代码仅获取用户的数字和一个小数点,这对我来说在 KeyPress 事件上工作得很好:

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点/小数点后的数字/数字。

更新我!

I am using the following code to take only digits from user and only one decimal point , that is working fine for me on 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;
}

Now I want to Limit the numbers/Digits after the decimal/dot i.e 35.25468, means it take only 6 numbers/digits after the dot/decimal.

Update me !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

作死小能手 2024-12-09 16:54:32
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;
        }

        }

    }

这段代码会对你有所帮助。它只需要一位小数和一位小数后两位,您可以相应地更改它。

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

        }

    }

This code will help you. It takes only one decimal place and two digit after one decimal place and you can change it accordingly.

无声无音无过去 2024-12-09 16:54:32

您可以添加一个额外的检查,如下所示

TextBox textBox = (TextBox) sender;

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

注意,子字符串将包含“.”因此检查是>=3

you can add an additional check like this

TextBox textBox = (TextBox) sender;

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

Note, the Substring will include the '.' and hence the check is >=3.

一身仙ぐ女味 2024-12-09 16:54:32

在按键事件和/或验证事件中,计算小数点后的字符数。按下按键时,将其抑制。验证时,删除多余的小数位。确保您从 NumberFormatInfo 获取小数点字符,并非所有区域性都使用“.”,即。在法国,小数点实际上是逗号

On the keypress event, and or validate event, count the number of chars after decimal point. On key press, suppress it. on validate, remove extra decimal places. Make sure you're getting the decimal point char from NumberFormatInfo, not all cultures use '.', ie. in France, their decimal point is actually a comma

忆梦 2024-12-09 16:54:32

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

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

这种特殊格式会截掉小数点后第三位数字。

On keypress, format the string and set the textBox.Text to the formatted string.

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

This particular format cuts off the number at the 3rd decimal.

自由范儿 2024-12-09 16:54:32

我有 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;
        }
    }
}

I had textBox.SelectionLength == 0 to allow the modification of selected text:

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;
        }
    }
}
峩卟喜欢 2024-12-09 16:54:32

我对 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)}";
        }
    }

The issue I have with the answer of Both FM is that you cannot edit the text when you have entered a decimal place and two decimals.

This code also takes a minus amount.

    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 = 
quot;-{((TextBox)sender).Text.Replace("-", string.empty)}";
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文