473,383 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Numeric values in textbox.

Hello,

how do you create a textbox control that can only hold numeric values?

Thanks,

Qwert.
Nov 21 '05 #1
7 1965
Simple google search turns up this as the first listing:
http://www.a1vbcode.com/snippet.asp?ID=2408

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
e.Handled = TrapKey(Asc(e.KeyChar))
End Sub

Private Function TrapKey(ByVal KCode As String) As Boolean
If (KCode >= 48 And KCode <= 57) Or KCode = 8 Then
TrapKey = False
Else
TrapKey = True
End If
End Function

Chris

"Qwert" <no**@nosp.com> wrote in message
news:A9********************@casema.nl...
Hello,

how do you create a textbox control that can only hold numeric values?

Thanks,

Qwert.

Nov 21 '05 #2
Hi Qwert
Simply you can create a class that inherit the windows form textbox
control . and modify the get string to accept ony numeric character
otherwise reaise exception , show error message , or whatever you want your
logic to be . then use that class into your form . check also this on how
to detect a number entered http://www.a1vbcode.com/snippet.asp?ID=2408
Hope this helps
Mohamed M .Mahfouz
Developer Support Engineer
ITWorx on behalf of Microsoft EMEA GTSC

Nov 21 '05 #3
"Qwert" <no**@nosp.com> schrieb:
how do you create a textbox control that can only hold numeric values?


<URL:http://groups.google.de/groups?threadm=Df4xd.14%24vb7.4%40fe1.columbus.rr. com>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4
This code is what I wrote a few years ago. It accepts numbers, in 1 decimal
point & 1 minus sign too:

Private Sub NumericTextBox_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
Dim KeyAscii As Integer
KeyAscii = Asc(e.KeyChar)

Select Case KeyAscii
Case 48 To 57, 8, 13
Case 45
If InStr(Me.Text, "-") <> 0 Then
KeyAscii = 0
End If
If Me.SelectionStart <> 0 Then
KeyAscii = 0
End If
Case 46
If InStr(Me.Text, ".") <> 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select

If KeyAscii = 0 Then
e.Handled = True
Else
e.Handled = False
End If
End Sub

I hope this helps
Nov 21 '05 #5
"Crouchie1998" <Cr**********@discussions.microsoft.com> schrieb:
This code is what I wrote a few years ago. It accepts numbers, in 1
decimal
point & 1 minus sign too:


.... but it won't prevent the user from pasting text from the clipboard :-).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #6
I haven't tested the CTRL + V method to tell you the truth.
Nov 21 '05 #7
Thank you all.

I turned into this:

Private Sub TextBoxNumericOnly(ByRef strTextBox As TextBox, ByRef e As
System.Windows.Forms.KeyPressEventArgs, Optional ByVal blnAllowNegative As
Boolean = False, Optional ByVal blnAllowDecimal As Boolean = False)

Try
With strTextBox
e.Handled = True
Select Case Convert.ToInt32(e.KeyChar)
Case 48 To 57, 8, 13 ' 0 to 9, backspace, enter, delete.
e.Handled = False
Case 45 '-'.
REM You may only add 1 '-' char and only if it it
the first.
If blnAllowNegative Then
If (.Text.Length - .SelectedText.Length) = 0
Then
If .Text.LastIndexOf("-"c) = -1 Then
e.Handled = False
End If
End If
End If
Case 46 '.'.
REM You may only add 1 '.' char.
If blnAllowDecimal Then
If .Text.LastIndexOf("."c) = -1 Then
e.Handled = False
End If
End If
End Select
End With
Catch Ex As Exception
ExToMsg(Ex, mstrMessage)
End Try

End Sub

but you can still press CTRL+V or use rightclick menu paste or when you
select all text you can't press the '-' or '.' when the selected text
contains one of them even though it would be replaced.
"Crouchie1998" <Cr**********@discussions.microsoft.com> schreef in bericht
news:98**********************************@microsof t.com...
I haven't tested the CTRL + V method to tell you the truth.

Nov 21 '05 #8

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

Similar topics

1
by: Jose Gonzalez | last post by:
How to apply a numeric format to a textbox using xhtml? I know you have to use the "-wap-input-format" style tag in css. I can get this to work in a regular xhtml page, however, I've been...
2
by: anonieko | last post by:
This applies to javascript dynamic textbox onkey > > > Newsgroups: comp.lang.javascript From: Lasse Reichstein Nielsen <l...@hotpop.com> - Find messages by this author Date: Fri, 15 Jul 2005...
16
by: Keith | last post by:
Am I crazy - to be shocked that there is no Numeric, Alpha, and AlphaNumeric Property in on the Textbox control I searched and searched - and found other people's code - but they don't work...
11
by: Keith | last post by:
I apologize for those of you who think I'm posting on the same topic. It is not that I don't appreciate all of your comments - and I'm definitely reading them all - but I think I have a differing...
1
by: Luqman | last post by:
My textbox is bound to Typed Dataset, with a Numeric Field, I want the user should not type anything in textbox except Numeric, how can I ? The data type is : Sql Server Decimal(5,2) Currently,...
5
tolkienarda
by: tolkienarda | last post by:
hi all i am trying to stop people from entering anything but numbers into a textbox. i have the code to make sure they have entered a number on each keypress event and now a msgbox appears if they...
7
by: nussu | last post by:
Hi, Plz provide me javascript : javascript for a textbox to accept the values in decimals and within range i need to enter a value in textbox like 1.03 and it should be <3 (lessthan 3). Plz...
6
by: robintwos | last post by:
Hi I would like to validate the while entering the data in a textbox for numeric values. it means . i would like to throw an error message when the user enters a character value. validation...
5
lotus18
by: lotus18 | last post by:
Hello World! I have a sample code here written in vb .net that restricts the textbox to accept only alpha, alphanumeric or numeric characters. Public Enum MyOption Alpha = 1 ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.