Home > Net >  Format textbox with numbers only
Format textbox with numbers only

Time:11-12

I have a textbox that accepts only numbers, no other characters. And I created the following function in the keypress method for that:

    private void txtRGIE_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!Char.IsDigit(e.KeyChar) && e.KeyChar != (char)8)
        {
            e.Handled = true;
        }
    }

Validation is working when I type, I can't type special characters or letters like I wanted. However, if I copy a numeric string that contains dots or other characters and paste it into the field, it accepts normally. For example, if you copy: 323.323 / 323 and paste into the field, it will accept. How do I validate the characters I paste, allowing only numbers?

CodePudding user response:

I have a textbox that accepts only numbers

And that's the flaw; saying "I have a knife here that i'm trying to use as a screwdriver, but i keep cutting myself with it, so i filed it smooth, but it's too big to get into the screw hole, so I filed it small, but it doesn't turn a shaped screw very well, and the tip isn't hardened so it keeps breaking.."

The answer is to use a shaped screwdriver, rather than keep repeatedly trying to kludge something not made for the job, into something that will do the job

A NumericUpDown control is the right tool for this job; it accepts only numbers, has configurable decimal places, and upper and lower limits, cannot have alphameric text typed or pasted into it and, bonus, the user can use the Up and Down cursor keys to change the value

NUD is a drop in replacement for your textbox, it's free and it's part of the standard lib so there isn't anything to install - just remember to get the .Value, not the .Text, and that it's a decimal, so you might want to cast it to something else to use it (double? int?) depending on what your app expects

If you don't like the little up down buttons, see here

CodePudding user response:

you can use :

private void txtRGIE_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;
   }
}

or you can use a NumericUpDown instead refer to this answers so you understand more.

CodePudding user response:

You could use a MaskedTextBox instead of a regular one.

CodePudding user response:

As already mentioned a NumericUpDown control is a good choice and to make it appear like a TextBox you can hide the up/down arrows e.g.

amountNumericUpDown1.Controls[0].Hide();

Or create a custom version with no up/down arrows and in this case no beep when pressing enter key.

public class SpecialNumericUpDown : NumericUpDown
{

    public SpecialNumericUpDown()
    {
        Controls[0].Hide();
        TextAlign = HorizontalAlignment.Right;
    }
    protected override void OnTextBoxResize(object source, EventArgs e)
    {
        Controls[1].Width = Width - 4;
    }

    public delegate void TriggerDelegate();

    public event TriggerDelegate TriggerEvent;
    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.KeyCode == (Keys.Return))
        {
            e.Handled = true;
            e.SuppressKeyPress = true;

            TriggerEvent?.Invoke();

            return;
        }

        base.OnKeyDown(e);
    }

}
  • Related