Hi to all!
I need help with this little problem. I want to input only numbers into the textbox and comma for decimals, but i cannot come with an idea. Please help
3 replies to this topic
#1
Posted 11 September 2010 - 06:44 AM
|
|
|
#2
Posted 11 September 2010 - 07:17 AM
You can attach an event handler to the key press event of the text box and reject all the characters you don't want. You could also use a MaskedTextBox which allows you to specify the input format (and won't allow other things).
#3
Posted 11 September 2010 - 07:40 AM
how to reject characters i dont want? can u please show some code with it
#4
Posted 11 September 2010 - 08:56 AM
You must check if the character is valid in the KeyPress event handler, and if it is not, set the Handled attribute of the event to true. This prevents to continue processing the event, so the textbox doesn't write the new character.
This code shows an example:
This code shows an example:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (((e.KeyChar < '0') || (e.KeyChar > '9')) && (e.KeyChar != ','))
{
e.Handled = true;
}
}
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









