How to make an number only text box?
-
-
Hi all: I am doing a windows form application. I need to some textboxes which can only be inserted numbers like prices. Is there any existing component in windows application of C# please? What's the best way of achieving this please? Thanks alot
Asura
-
Hi all: I am doing a windows form application. I need to some textboxes which can only be inserted numbers like prices. Is there any existing component in windows application of C# please? What's the best way of achieving this please? Thanks alot
Asura
Greeeg is definitely right, but I'd prefer a slightly different way than using exception. Here's the code:
private void AmountBox_KeyPress(object sender, KeyPressEventArgs e) { switch (e.KeyChar) { case '0': break; case '1': break; case '2': break; case '3': break; case '4': break; case '5': break; case '6': break; case '7': break; case '8': break; case '9': break; default: e.Handled = true; break; } }
Setting Handled to true prevents the input from being processed further and that is exactly what you want for anything else than numbers. BTW: This is a function for the KeyPress event. Greetings -
Greeeg is definitely right, but I'd prefer a slightly different way than using exception. Here's the code:
private void AmountBox_KeyPress(object sender, KeyPressEventArgs e) { switch (e.KeyChar) { case '0': break; case '1': break; case '2': break; case '3': break; case '4': break; case '5': break; case '6': break; case '7': break; case '8': break; case '9': break; default: e.Handled = true; break; } }
Setting Handled to true prevents the input from being processed further and that is exactly what you want for anything else than numbers. BTW: This is a function for the KeyPress event. Greetings