Home All Groups Group Topic Archive Search About

allowing just numeric value in my textbox

Author
30 Jul 2006 9:09 AM
Reny
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)

Author
30 Jul 2006 11:02 AM
Lars Graeve
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


Show quoteHide quote
"Reny" <r***@bxtech.com> schrieb im Newsbeitrag
news:ey8Sdf7sGHA.2036@TK2MSFTNGP05.phx.gbl...
> 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)
>
>
Author
30 Jul 2006 12:14 PM
YardDancer
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



Show quoteHide quote
"Reny" <r***@bxtech.com> wrote in message
news:ey8Sdf7sGHA.2036@TK2MSFTNGP05.phx.gbl...
> 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)
>
>
Author
30 Jul 2006 1:28 PM
Dennis
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


Show quoteHide quote
"YardDancer" wrote:

> 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" <r***@bxtech.com> wrote in message
> news:ey8Sdf7sGHA.2036@TK2MSFTNGP05.phx.gbl...
> > 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)
> >
> >
>
>
>
Author
30 Jul 2006 3:26 PM
Kerry Moorman
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


Show quoteHide quote
"Reny" wrote:

> 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)
>
>
>
Author
31 Jul 2006 3:11 PM
Claes Bergefall
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

Show quoteHide quote
"Reny" <r***@bxtech.com> wrote in message
news:ey8Sdf7sGHA.2036@TK2MSFTNGP05.phx.gbl...
> 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)
>
>