Torna al Thread
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim tasto As Integer = Asc(e.KeyChar)
If Me.control(tasto, TextBox1) Then
e.Handled = True
End If
End Sub
Private Function control(ByVal tasto As Integer, ByVal txtbox As TextBox) As Boolean
' Da 48 a 57 sono numeri
' 46 è il punto
' 127 DEL
' 8 BACKSPACE
If Not ((tasto >= 48) And (tasto <= 57) Or (tasto = 127) Or (tasto = 8)) Then
' Con numeri, DEL, BACKSPACE tutto ok
' La virgola la posso mettere solo una volta
If tasto = 46 Then
If Me.findCharInText(".", txtbox) Then
Return True
Else
Return False
End If
End If
Return True
Else
Return False
End If
End Function
'
' Funzione che trova un carattere in una stringa
'
Private Function findCharInText(ByVal chr As Char, ByVal txtbox As TextBox) As Boolean
If txtbox.Text.IndexOf(chr) >= 0 Then ' Trovato
Return True
Else ' Non trovato
' Non voglio permettere l'inserimento della virgola in primo posto
' Quindi se il primo carattere è un virgola allora non devo inserirla
If txtbox.Text.Length = 0 Then
Return True
End If
Return False
End If
End Function