Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

allowing just numeric value in my textbox

1 view
Skip to first unread message

Reny

unread,
Jul 30, 2006, 5:09:35 AM7/30/06
to
can any one tell how can i restrict my user to type just numeric character
in the textbox.I am using VS.NET 2003 (VB.NET)


Lars Graeve

unread,
Jul 30, 2006, 7:02:56 AM7/30/06
to
Hi

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...

YardDancer

unread,
Jul 30, 2006, 8:14:42 AM7/30/06
to
Hi Rene,

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...

Dennis

unread,
Jul 30, 2006, 9:28:01 AM7/30/06
to
The input should still be checked when the control loses focus to be sure the
user has not "cut and pasted" something other than numeric in the text box.
--
Dennis in Houston

Kerry Moorman

unread,
Jul 30, 2006, 11:26:01 AM7/30/06
to
Reny,

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

Claes Bergefall

unread,
Jul 31, 2006, 11:11:53 AM7/31/06
to
Public Class NumericTextBox
Inherits TextBox

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...

0 new messages