giovedì 8 agosto 2013

C# - Writing Only Number in a TextBox

If you have a Text Box and you want that your users write only numbers, you can use the following code:




private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) &&                                     !char.IsPunctuation(e.KeyChar))
                e.Handled = true;
        }


How you can understand the code is in the "KeyPress" event of the TextBox. 



My Two Cents ... 

Nessun commento:

Posta un commento