Controllare inserimento numerico nella text




Dim KeyAscii As Short = Asc(e.KeyChar)

' In questo caso oltre a consentire numeri, tasto Canc

' e tasto BackSpace, devo consentire anche l'immissione

' del punto e della virgola

If KeyAscii < 48 And KeyAscii <> 24 And KeyAscii <> 8 And e.KeyChar <> "." And e.KeyChar <> "," Then
KeyAscii = 0
ElseIf KeyAscii > 57 Then
KeyAscii = 0
End If

' Faccio in modo che se l'utente digita la virgola

' mi appaia il punto

If e.KeyChar = "," Then
KeyAscii = 46 ' 46 e' il codice ascii del punto
End If

' Il punto e' si consentito

' ma non come primo carattere

If TextBox3.TextLength = 0 And KeyAscii = 46 Then
KeyAscii = 0
End If

' ovviamente se c'e' gia' un punto

' non e' consentito digitarne altri

If (KeyAscii = 46) And _
TextBox3.Text.IndexOf(".") > 0 Then
KeyAscii = 0
End If

' Reimposto il keychar

e.KeyChar = Chr(KeyAscii)

End Sub



https://www.settorezero.com/wordpress/vbnet-accettare-in-una-textbox-soltanto-numeri-o-caratteri-predefiniti/










( controllareinserimentonumericonellatext.html )- by Paolo Puglisi - Modifica del 17/12/2023