Torna al Thread
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
' Questo evento lo gestisco unicamente per cancellare lo zero nella textbox
' appena arrivo a scrivere, consento soltanto l'immissione di numeri (ascii 48-57)
Dim KeyAscii As Integer = Asc(e.KeyChar) ' codice ascii del tasto premuto
' Il carattere premuto è diverso da un numero e diverso da backspace?
If (KeyAscii < 48 And KeyAscii <> 8) Or KeyAscii > 57 Then
e.KeyChar = Chr(0) ' non scrivo niente nella textbox
ElseIf KeyAscii = 8 Then ' backspace?
e.KeyChar = Chr(8) ' scrivo backspace (cancello il carattere precedente)
Else
If TextBox1.Text = "0" Then ' nella textbox c'è scritto zero?
TextBox1.Text = Chr(KeyAscii) ' si, cancello lo zero per permettermi di scrivere
e.Handled = True
End If
End If
End Sub