I would use an own text box like this one:
Public Class NumericTextBox
Inherits TextBox
Private allowedChars As New Collections.Generic.List(Of Char)(Chr(13) &
Chr(27) & Chr(8) & "1234567890.-".ToCharArray)
Private Sub NumericTextBox_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
' Allow charactes only, which are in a list
If Not allowedChars.Contains(e.KeyChar) Then e.KeyChar = Nothing
End Sub
Private Sub NumericTextBox_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles Me.Validating
e.Cancel = Not IsNumeric(Me.Text)
End Sub
End Class
Of course you can catch those two events of a standard text box, too, and
handle them the same way. But putting it into a inherited class brings the
advantage to be able to used this function a multiple time. Maybe you make
even a user control out of it.
Lars
"Reny" <re...@bxtech.com> schrieb im Newsbeitrag
news:ey8Sdf7s...@TK2MSFTNGP05.phx.gbl...
The best approach in my opinion is to restrict user entry by using the
keypress event of the textbox
Example. that only allows digits 1 to 0 and backspace key entries.
Private Sub txtPhoneNumber_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) _ Handles txtPhoneNumber.KeyPress
Select Case Asc(e.KeyChar)
Case 8, 48 To 57 ' all chars from 0 to 9 and backspace
e.Handled = False
Case Else
e.Handled = True
End Select
End Sub
you could look up the ASCII codes for decimal, and commas if you you like.
Yard Dancer
"Reny" <re...@bxtech.com> wrote in message
news:ey8Sdf7s...@TK2MSFTNGP05.phx.gbl...
I think the most user-friendly way is to use the textbox's Validating event.
In the Validating event you can use VB's IsNumeric function to test the
input. If the input is not numeric you can use a message box or an error
provider to inform the user and not allow them to leave the textbox without
supplying a numeric value.
Kerry Moorman
Private Const ES_NUMBER As Integer = &H2000
Protected Overrides ReadOnly Property CreateParams() As
System.Windows.Forms.CreateParams
Get
Dim params As CreateParams = MyBase.CreateParams
params.Style = params.Style Or ES_NUMBER
Return params
End Get
End Property
Protected Overrides Function ProcessCmdKey(ByRef msg As
System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As
Boolean
'Need to prevent pasting of non-numeric characters
If keyData = (Keys.Shift Or Keys.Insert) OrElse keyData =
(Keys.Control Or Keys.V) Then
Dim data As IDataObject = Clipboard.GetDataObject
If data Is Nothing Then
Return MyBase.ProcessCmdKey(msg, keyData)
Else
Dim text As String =
CStr(data.GetData(DataFormats.StringFormat, True))
If text = String.Empty Then
Return MyBase.ProcessCmdKey(msg, keyData)
Else
For Each ch As Char In text.ToCharArray
If Not Char.IsNumber(ch) Then
Return True
End If
Next
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End If
Else
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End Function
End Class
/claes
"Reny" <re...@bxtech.com> wrote in message
news:ey8Sdf7s...@TK2MSFTNGP05.phx.gbl...