2

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 !

CAbbott
  • 8,078
  • 4
  • 31
  • 38
Shahid Ghafoor
  • 2,991
  • 17
  • 68
  • 123
  • 1
    Whereas it's possible to do this, I would strongly argue against it, as it's very frustrating for users if they try to edit the field. It also complicates your code considerably. You're better off all around if you validate after the user has exited the field or submitted the form. Use the built-in validation stuff. That's what it's for. – Jim Mischel Aug 31 '11 at 15:37

6 Answers6

4
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.

slezadav
  • 6,104
  • 7
  • 40
  • 61
Both FM
  • 770
  • 1
  • 14
  • 33
0

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;
        }
    }
}
Kevin.Debeil
  • 349
  • 3
  • 8
0

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 = $"-{((TextBox)sender).Text.Replace("-", string.empty)}";
        }
    }
Jos R.
  • 41
  • 4
0

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

msarchet
  • 15,104
  • 2
  • 43
  • 66
Mark Menchavez
  • 1,651
  • 12
  • 15
  • 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 – Mark Menchavez Aug 31 '11 at 15:17
0

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.

Bala R
  • 107,317
  • 23
  • 199
  • 210
0

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.

N_A
  • 19,799
  • 4
  • 52
  • 98