Thank you all.
I turned into this:
Private Sub TextBoxNumericOnly(ByRef strTextBox As TextBox, ByRef e As
System.Windows.Forms.KeyPressEventArgs, Optional ByVal blnAllowNegative As
Boolean = False, Optional ByVal blnAllowDecimal As Boolean = False)
Try
With strTextBox
e.Handled = True
Select Case Convert.ToInt32(e.KeyChar)
Case 48 To 57, 8, 13 ' 0 to 9, backspace, enter, delete.
e.Handled = False
Case 45 '-'.
REM You may only add 1 '-' char and only if it it
the first.
If blnAllowNegative Then
If (.Text.Length - .SelectedText.Length) = 0
Then
If .Text.LastIndexOf("-"c) = -1 Then
e.Handled = False
End If
End If
End If
Case 46 '.'.
REM You may only add 1 '.' char.
If blnAllowDecimal Then
If .Text.LastIndexOf("."c) = -1 Then
e.Handled = False
End If
End If
End Select
End With
Catch Ex As Exception
ExToMsg(Ex, mstrMessage)
End Try
End Sub
but you can still press CTRL+V or use rightclick menu paste or when you
select all text you can't press the '-' or '.' when the selected text
contains one of them even though it would be replaced.
"Crouchie1998" <Cr**********@discussions.microsoft.com> schreef in bericht
news:98**********************************@microsof t.com...
I haven't tested the CTRL + V method to tell you the truth.