In VB.NET Windows Form, how do I set my text box to only allow numbers?
Text Box only allow numbers.
Collapse
This topic is closed.
X
X
-
Mike LTags: None -
mikeh
Re: Text Box only allow numbers.
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
"Mike L" <Cadel@nospam.n ospam> wrote in message
news:F6949B7F-AB81-46BD-B014-988B3ECACCCE@mi crosoft.com...[color=blue]
> In VB.NET Windows Form, how do I set my text box to only allow numbers?[/color]
-
Crouchie1998
Re: Text Box only allow numbers.
But that won't stop the user pressing CTRL + V to paste in letters
Crouchie1998
BA (HONS) MCP MCSE
Comment
-
Cor Ligthert
Re: Text Box only allow numbers.
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.
I hope this helps,
Cor
Comment
-
Herfried K. Wagner [MVP]
Re: Text Box only allow numbers.
"Mike L" <Cadel@nospam.n ospam> schrieb:[color=blue]
> In VB.NET Windows Form, how do I set my text box to only allow numbers?[/color]
Place an ErrorProvider component on the form and add this code:
\\\
Imports System.Componen tModel
..
..
..
Private Sub TextBox1_Valida ting( _
ByVal sender As Object, _
ByVal e As CancelEventArgs _
) Handles TextBox1.Valida ting
Dim SourceControl As TextBox = DirectCast(send er, TextBox)
Dim ErrorText As String
Try
Dim i As Integer = Integer.Parse(S ourceControl.Te xt)
Catch
ErrorText = "Value must be an integer."
Finally
Me.ErrorProvide r1.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/>
Comment
-
Jack Russell
Re: Text Box only allow numbers.
Herfried K. Wagner [MVP] wrote:[color=blue]
> "Mike L" <Cadel@nospam.n ospam> schrieb:
>[color=green]
>> In VB.NET Windows Form, how do I set my text box to only allow numbers?[/color]
>
>
> Place an ErrorProvider component on the form and add this code:
>
> \\\
> Imports System.Componen tModel
> ..
> ..
> ..
> Private Sub TextBox1_Valida ting( _
> ByVal sender As Object, _
> ByVal e As CancelEventArgs _
> ) Handles TextBox1.Valida ting
> Dim SourceControl As TextBox = DirectCast(send er, TextBox)
> Dim ErrorText As String
> Try
> Dim i As Integer = Integer.Parse(S ourceControl.Te xt)
> Catch
> ErrorText = "Value must be an integer."
> Finally
> Me.ErrorProvide r1.SetError( _
> SourceControl, _
> ErrorText _
> )
> End Try
> End Sub
> ///
>[/color]
or buy Farpoint's Inputpro - no connection other than as a customer
Comment
-
Al Reid
Re: Text Box only allow numbers.
"Mike L" <Cadel@nospam.n ospam> wrote in message news:F6949B7F-AB81-46BD-B014-988B3ECACCCE@mi crosoft.com...[color=blue]
> In VB.NET Windows Form, how do I set my text box to only allow numbers?[/color]
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
Comment
-
mikeh
Re: Text Box only allow numbers.
Don't mean to be sarcastic, but did you even try? It most certainly does
stop a CTRL+V paste.
"Crouchie19 98" <crouchie1998@s pamcop.net> wrote in message
news:%23PrfG2Gf FHA.3296@TK2MSF TNGP10.phx.gbl. ..[color=blue]
> But that won't stop the user pressing CTRL + V to paste in letters
>
>[/color]
http://www.planet-source-code.com/vb...=932&lngWId=10[color=blue]
>
>[/color]
http://www.planet-source-code.com/vb...2662&lngWId=10[color=blue]
>
>[/color]
http://www.planet-source-code.com/vb...1213&lngWId=10[color=blue]
>
>[/color]
http://www.planet-source-code.com/vb...3481&lngWId=10[color=blue]
>
> http://www.codeproject.com/useritems/advtextbox.asp
>
> Crouchie1998
> BA (HONS) MCP MCSE
>
>[/color]
Comment
-
mikeh
Re: Text Box only allow numbers.
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" <mikeh@forbizte ch.com> wrote in message
news:O0o10DOfFH A.3848@TK2MSFTN GP15.phx.gbl...[color=blue]
> Don't mean to be sarcastic, but did you even try? It most certainly does
> stop a CTRL+V paste.
>
> "Crouchie19 98" <crouchie1998@s pamcop.net> wrote in message
> news:%23PrfG2Gf FHA.3296@TK2MSF TNGP10.phx.gbl. ..[color=green]
> > But that won't stop the user pressing CTRL + V to paste in letters
> >
> >[/color]
>[/color]
http://www.planet-source-code.com/vb...=932&lngWId=10[color=blue][color=green]
> >
> >[/color]
>[/color]
http://www.planet-source-code.com/vb...2662&lngWId=10[color=blue][color=green]
> >
> >[/color]
>[/color]
http://www.planet-source-code.com/vb...1213&lngWId=10[color=blue][color=green]
> >
> >[/color]
>[/color]
http://www.planet-source-code.com/vb...3481&lngWId=10[color=blue][color=green]
> >
> > http://www.codeproject.com/useritems/advtextbox.asp
> >
> > Crouchie1998
> > BA (HONS) MCP MCSE
> >
> >[/color]
>
>[/color]
Comment
Comment