Home All Groups Group Topic Archive Search About

Text Box only allow numbers.

Author
28 Jun 2005 7:20 PM
Mike L
In VB.NET Windows Form, how do I set my text box to only allow numbers?

Author
28 Jun 2005 8:06 PM
mikeh
You can test the keypress method of your text box for the value.  If it was
a numeric key then allow it if now then -->
e.handled = True


Show quoteHide quote
"Mike L" <Cadel@nospam.nospam> wrote in message
news:F6949B7F-AB81-46BD-B014-988B3ECACCCE@microsoft.com...
> In VB.NET Windows Form, how do I set my text box to only allow numbers?
Author
29 Jun 2005 7:30 PM
mikeh
Don't mean to be sarcastic, but did you even try?  It most certainly does
stop a CTRL+V paste.

"Crouchie1998" <crouchie1998@spamcop.net> wrote in message
news:%23PrfG2GfFHA.3296@TK2MSFTNGP10.phx.gbl...
> But that won't stop the user pressing CTRL + V to paste in letters
>
>
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=932&lngWId=10
>
>
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2662&lngWId=10
>
>
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=1213&lngWId=10
>
>
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3481&lngWId=10
Show quoteHide quote
>
> http://www.codeproject.com/useritems/advtextbox.asp
>
> Crouchie1998
> BA (HONS) MCP MCSE
>
>
Author
29 Jun 2005 7:34 PM
mikeh
But it won't stop a right mouse click paste.  :-(

I wasn't accounting for any kind of a user paste, so Herfried's suggestion
is a nice stop gap.

"mikeh" <mi***@forbiztech.com> wrote in message
news:O0o10DOfFHA.3848@TK2MSFTNGP15.phx.gbl...
> Don't mean to be sarcastic, but did you even try?  It most certainly does
> stop a CTRL+V paste.
>
> "Crouchie1998" <crouchie1998@spamcop.net> wrote in message
> news:%23PrfG2GfFHA.3296@TK2MSFTNGP10.phx.gbl...
> > But that won't stop the user pressing CTRL + V to paste in letters
> >
> >
>
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=932&lngWId=10
> >
> >
>
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2662&lngWId=10
> >
> >
>
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=1213&lngWId=10
> >
> >
>
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3481&lngWId=10
Show quoteHide quote
> >
> > http://www.codeproject.com/useritems/advtextbox.asp
> >
> > Crouchie1998
> > BA (HONS) MCP MCSE
> >
> >
>
>
Author
29 Jun 2005 6:14 AM
Cor Ligthert
Mike,

In my opinion is the most important thing in this that you use the keyup
event for when the user is typing and the validating event after that to
check that he has not pasted something in. Than you can make it as nice as
you with accoording to your own definition of numeric (By instance using
IsNumeric is an exponent an accepted part of a number).

In this message is a (basic) sample (showing something more) that I once
made, however this newsgroup is full of samples, in my opinion mostly better
than I have seen on otherplaces on internet for that.

http://groups-beta.google.com/group/microsoft.public.dotnet.languages.vb/msg/d0aa417bf7a25528?hl=en

I hope this helps,

Cor
Author
29 Jun 2005 8:42 AM
Herfried K. Wagner [MVP]
"Mike L" <Cadel@nospam.nospam> schrieb:
> In VB.NET Windows Form, how do I set my text box to only allow numbers?

Place an ErrorProvider component on the form and add this code:

\\\
Imports System.ComponentModel
..
..
..
Private Sub TextBox1_Validating( _
    ByVal sender As Object, _
    ByVal e As CancelEventArgs _
) Handles TextBox1.Validating
    Dim SourceControl As TextBox = DirectCast(sender, TextBox)
    Dim ErrorText As String
    Try
        Dim i As Integer = Integer.Parse(SourceControl.Text)
    Catch
        ErrorText = "Value must be an integer."
    Finally
        Me.ErrorProvider1.SetError( _
            SourceControl, _
            ErrorText _
        )
    End Try
End Sub
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
29 Jun 2005 8:56 AM
Jack Russell
Herfried K. Wagner [MVP] wrote:
Show quoteHide quote
> "Mike L" <Cadel@nospam.nospam> schrieb:
>
>> In VB.NET Windows Form, how do I set my text box to only allow numbers?
>
>
> Place an ErrorProvider component on the form and add this code:
>
> \\\
> Imports System.ComponentModel
> ..
> ..
> ..
> Private Sub TextBox1_Validating( _
>    ByVal sender As Object, _
>    ByVal e As CancelEventArgs _
> ) Handles TextBox1.Validating
>    Dim SourceControl As TextBox = DirectCast(sender, TextBox)
>    Dim ErrorText As String
>    Try
>        Dim i As Integer = Integer.Parse(SourceControl.Text)
>    Catch
>        ErrorText = "Value must be an integer."
>    Finally
>        Me.ErrorProvider1.SetError( _
>            SourceControl, _
>            ErrorText _
>        )
>    End Try
> End Sub
> ///
>
or buy Farpoint's Inputpro - no connection other than as a customer
Author
29 Jun 2005 12:16 PM
Al Reid
"Mike L" <Cadel@nospam.nospam> wrote in message news:F6949B7F-AB81-46BD-B014-988B3ECACCCE@microsoft.com...
> In VB.NET Windows Form, how do I set my text box to only allow numbers?

Perhaps using the Windows API is out of style with .Net, but you could grab the current window style of the textbox control using
GetWindowLong, OR the result with ES_NUMBER and set the window style using SetWindowLong.  That will handle both keyboard and paste
issues.

--

Al Reid