Se, come immagino, nella textbox devi inserire dei numeri con la virgola il codice che hai scritto non va bene perché, per esempio, ti permette di inserire anche più di una virgola...
Io uso una classe del genere:
codice:
Public Class CKeyFilterPunto
Public Sub OnlyNumbersAndComma(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim TB As TextBox
TB = CType(sender, TextBox)
Dim s As [String] = TB.Text.ToString()
Dim DecimalSeparator1 As String = ","
Dim Virgola As Integer = s.IndexOf(DecimalSeparator1.ToString(), 0, s.Length)
If Virgola = -1 Then
If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Or DecimalSeparator1.IndexOf(e.KeyChar) <> -1) Then
e.Handled = True
End If
Else
If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then
e.Handled = True
End If
End If
End Sub
End Class
Crea questa variabile a livello di Form:
codice:
Private CkfVirgolaEPunto As New CKeyFilterPuntoEVirgola
e la assegno alla KeyPress della Textbox
codice:
AddHandler TextBox.KeyPress, AddressOf Ckf.OnlyNumbersAndComma