''' <summary>
''' Nur Eingabe von Zahlen zulassen {0-9/,/-}
''' </summary>
''' <param name="T">Typ TextBox</param>
''' <param name="allow">Ein-/Ausschalten numerische Eingabe</param>
''' <remarks></remarks>
<Extension()> _
Public Sub AllowNumeric(ByVal T As TextBox, ByVal allow As Boolean)
If T IsNot Nothing Then
If allow Then
AddHandler T.KeyPress, AddressOf DoubleKeyPresser
Else
RemoveHandler T.KeyPress, AddressOf DoubleKeyPresser
End If
End If
End Sub
Private Sub DoubleKeyPresser(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Select Case e.KeyChar
Case ControlChars.Tab, ControlChars.Back : Exit Sub
Case ControlChars.Cr : e.Handled = True : SendKeys.Send("{TAB}") : Exit Sub
Case "0"c To "9"c : Exit Sub
Case "-"c
If _CountStrings(CType(sender, TextBox).Text, CChar("-")) = 0 Then Exit Sub
Case ","c ' nur maximal 1 Komma zulassen
If _CountStrings(CType(sender, TextBox).Text, CChar(",")) = 0 Then Exit Sub
Case "."c
If _CountStrings(CType(sender, TextBox).Text, CChar(",")) = 0 Then e.KeyChar = ","c : Exit Sub
End Select
e.Handled = True
End Sub
Private Function _CountStrings(ByVal str As String, ByVal regexStr As Char) As Integer
Dim regex As New RegularExpressions.Regex(regexStr)
Return regex.Matches(str).Count
End Function