473,396 Members | 1,766 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,396 software developers and data experts.

VB2005 question - numeric only textbox input

viv
how do you trap keyboard codes for text box in VB2005 ?
I'm looking for a code snippet to only allow numeric input in a text box
(same as Key event in VB6, then use Keyascii to decide action)

can anyone provide a snippet of code please

many thanks
Apr 25 '07 #1
4 6916
Dan,

You might want to use a MaskedTextbox control.

Or you could handle a textbox's KeyDown or KeyPress events.

Or, my personal preference, you could handle the textbox's Validating event.

Kerry Moorman
"viv" wrote:
how do you trap keyboard codes for text box in VB2005 ?
I'm looking for a code snippet to only allow numeric input in a text box
(same as Key event in VB6, then use Keyascii to decide action)

can anyone provide a snippet of code please

many thanks
Apr 25 '07 #2
viv pisze:
how do you trap keyboard codes for text box in VB2005 ?
I'm looking for a code snippet to only allow numeric input in a text box
(same as Key event in VB6, then use Keyascii to decide action)

can anyone provide a snippet of code please
try this control:
http://www.codeproject.com/vb/net/2gs_txtbx.asp#ext

jaro
Apr 26 '07 #3
here is my code for numeric textbox

Public Class NumTest
Inherits TextBox

Private Sub NumTest_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If IsNumeric(e.KeyChar) And Not e.KeyChar = "-" Then
If Not IsNumeric(Me.Text & e.KeyChar) Then
e.Handled = True
Exit Sub
End If
ElseIf e.KeyChar = "." Then
If Me.Text.IndexOf(".") <-1 Then
e.Handled = True
Exit Sub
End If
ElseIf e.KeyChar = "-" Then
If Me.SelectedText.Trim.Length = Me.Text.Trim.Length Then
Me.Text = "-0"
Me.SelectionStart = 1
Me.SelectionLength = 1
e.Handled = True
Else
If Me.SelectionStart <0 Or
Microsoft.VisualBasic.Left(Me.Text, 1) = "-" Then
e.Handled = True
End If
End If
ElseIf Not Char.IsControl(e.KeyChar) Then
e.Handled = True
End If
End Sub
Private Sub NumTest_Leave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Leave
If Me.Text.Length = 0 Then
Me.Text = "0"
End If
End Sub
End Class

On Wed, 25 Apr 2007 20:11:19 +1000, "viv" <lwrote:
>how do you trap keyboard codes for text box in VB2005 ?
I'm looking for a code snippet to only allow numeric input in a text box
(same as Key event in VB6, then use Keyascii to decide action)

can anyone provide a snippet of code please

many thanks
Apr 27 '07 #4
In addition to the many fine answers you have received, you could use a
masked textbox instead of a textbox, and set the format to #. Then they
will only be able to put in numbers.

Robin S.
---------------------------------
"viv" <lwrote in message
news:46***********************@news.optusnet.com.a u...
how do you trap keyboard codes for text box in VB2005 ?
I'm looking for a code snippet to only allow numeric input in a text box
(same as Key event in VB6, then use Keyascii to decide action)

can anyone provide a snippet of code please

many thanks

May 1 '07 #5

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...
2
by: Patrick De Ridder | last post by:
MSDN shows the option: textBox1.Numeric = true; as a way to check that the input in textBox1 is numeric. I cannot get it to work. How should it be done? Alternatively: looking at Validation in...
2
by: Sam | last post by:
Why isn't there a numberBox Windows Form control like a textbox control but for numeric (float) input? There doesn't seem to be any easy or visual solution to this VERY SIMPLE ISSUE in either...
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...
5
by: ScareCrow | last post by:
How do I go about limiting a textbox to accepting numeric key input from the keyboard in vb.net? --------------= Posted using GrabIt =---------------- ------= Binary Usenet downloading...
2
by: Chad | last post by:
I have a textbox control, txtMeasurement, that I want to allow only numeric decimal input. I thought to use a client side validation control to ensure that the data entered is of type "Double". ...
5
by: Reny | last post by:
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)
3
by: =?Utf-8?B?UmljaA==?= | last post by:
Hello, I selected BorderStyle = Fixed3D for a textbox on a form in VB2005, but the textbox looks flat. In VB2003 the textbox has the same borderStyle = Fixed3D and has a 3D appearance. Is it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.