Limit Numbers after Decimal on Key Press Event

2019-03-31 19:53发布

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 !

5条回答
再贱就再见
2楼-- · 2019-03-31 20:22
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.

查看更多
一纸荒年 Trace。
3楼-- · 2019-03-31 20:32

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;
        }
    }
}
查看更多
甜甜的少女心
4楼-- · 2019-03-31 20:44

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

查看更多
祖国的老花朵
5楼-- · 2019-03-31 20:45

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.

查看更多
一纸荒年 Trace。
6楼-- · 2019-03-31 20:47

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.

查看更多
登录 后发表回答