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);
}
Popularity: 8%
