473,566 Members | 3,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

allowing just numeric value in my textbox

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)
Jul 30 '06 #1
5 13074
Hi

I would use an own text box like this one:

Public Class NumericTextBox
Inherits TextBox

Private allowedChars As New Collections.Gen eric.List(Of Char)(Chr(13) &
Chr(27) & Chr(8) & "1234567890 .-".ToCharArr ay)

Private Sub NumericTextBox_ KeyPress(ByVal sender As Object, ByVal e As
System.Windows. Forms.KeyPressE ventArgs) Handles Me.KeyPress

' Allow charactes only, which are in a list
If Not allowedChars.Co ntains(e.KeyCha r) Then e.KeyChar = Nothing

End Sub

Private Sub NumericTextBox_ Validating(ByVa l sender As Object, ByVal e As
System.Componen tModel.CancelEv entArgs) Handles Me.Validating

e.Cancel = Not IsNumeric(Me.Te xt)

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.co mschrieb im Newsbeitrag
news:ey******** ******@TK2MSFTN GP05.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)


Jul 30 '06 #2
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.KeyPressE ventArgs) _ 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.co mwrote in message
news:ey******** ******@TK2MSFTN GP05.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)


Jul 30 '06 #3
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
"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.KeyPressE ventArgs) _ 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.co mwrote in message
news:ey******** ******@TK2MSFTN GP05.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)


Jul 30 '06 #4
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
"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)
Jul 30 '06 #5
Public Class NumericTextBox
Inherits TextBox

Private Const ES_NUMBER As Integer = &H2000

Protected Overrides ReadOnly Property CreateParams() As
System.Windows. Forms.CreatePar ams
Get
Dim params As CreateParams = MyBase.CreatePa rams
params.Style = params.Style Or ES_NUMBER
Return params
End Get
End Property

Protected Overrides Function ProcessCmdKey(B yRef 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.GetDa taObject
If data Is Nothing Then
Return MyBase.ProcessC mdKey(msg, keyData)
Else
Dim text As String =
CStr(data.GetDa ta(DataFormats. StringFormat, True))
If text = String.Empty Then
Return MyBase.ProcessC mdKey(msg, keyData)
Else
For Each ch As Char In text.ToCharArra y
If Not Char.IsNumber(c h) Then
Return True
End If
Next
Return MyBase.ProcessC mdKey(msg, keyData)
End If
End If
Else
Return MyBase.ProcessC mdKey(msg, keyData)
End If
End Function
End Class

/claes

"Reny" <re**@bxtech.co mwrote in message
news:ey******** ******@TK2MSFTN GP05.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)


Jul 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4140
by: Iona | last post by:
okay.. this should be long.. I made up a table in database with access consists of columns with text type. Some of them I put in data with numeric value and some of them I put in "Unlimited" as the value. Then, I allowed people to insert data but only with conditions. They can insert data if the value they are trying to insert is smaller...
5
13845
by: ief | last post by:
hi all, i'm trying to check the length of a numeric value in a string. this is what i need to do: I have a string "Mystring (253)" and a string "SecondString (31548745754)" Now i have to check if the string contains more than 3 numeric characters because i must only process the ones with more than 3.
3
1703
by: eric_caron_31 | last post by:
Here's my problem, I read numeric value and I want to display this value like this : 123 678. Value read : 123678 Value display : 123 678 I want space for separator thanks for your help
5
13484
by: SKS | last post by:
hi all i would like to know how to restrict user from entering numeric value ina textbox..using C# if user try to enter text it shouldnt be allowed.. i tried errror provider.. but it doesnt met my requirement.. thanks in advance for ur valuable reply let me know the best to do it urs
13
14953
by: nishit.gupta | last post by:
Is their any fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" Thnx
1
1205
by: karthik2423 | last post by:
Hi, Please let me know how I can accomplish the following task - "Ask the user to enter a number". Check whether the user has entered only numeric values. If he/she enters any character other than 0-9, it should print out an error. This is the program I have written so far.. #!/usr/bin/perl use strict; use warnings;
6
2905
by: frohlinger | last post by:
Hi, I need to perform some numeric calculations on a numeric float value, that is received as wstring. I would like to perform a check before converting the wstring to float, checking that indeed the wstring contains a numeric value. This is the actual conversion: double fValue = _wtof(strValue.c_str()); if strValue contains some characters...
1
4035
by: Ronald S. Cook | last post by:
We have a table "Animal" that contains the column "AnimalNumber" with values like these: 100001 100002 100003 1XAB 10E43ABC 11RU5V
2
2513
by: tron_23 | last post by:
hi, we use Toplink (TopLink - 4.6.0 (Build 417) with a DB2 Database 7.2. i know really old versions, but we could change to e newer one ;-) Sometimes we got some problems with update or insert statements -- numeric value out of range. The SQL statements should be right. We took the statements and we tried to execute it manually. It...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.