Accettare solo numeri nella textbox C#

Per gestire il testo consentito nella textbox utilizzando C# è necessario gestire l’evento KeyPress.

Per accettare solo numeri ed ovviamente la loro cancellazione questa funzione può essere d’aiuto.
Uno snippet utile (spero).

private bool checkForNumeric(KeyPressEventArgs e)
{
const char Delete = (char)8;
return !Char.IsDigit(e.KeyChar) && e.KeyChar != Delete;
}

Utilizzatela così

private void textbox_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = checkForNumeric(e);
}

Related Posts with Thumbnails

Popularity: 8%

Share this Post:
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

No Responses to “Accettare solo numeri nella textbox C#”

Leave a Reply:

Name (required):
Mail (will not be published) (required):
Website:
Comment (required):
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Indirizzo per il Trackback