Jump to content

textBox number problem

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Zloduh

Zloduh

    Newbie

  • Members
  • Pip
  • 2 posts
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

#2
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
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
Zloduh

Zloduh

    Newbie

  • Members
  • Pip
  • 2 posts
how to reject characters i dont want? can u please show some code with it

#4
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
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:

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