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

VB6.0 : Set textbox to Numeric

wassup
34
Good morning (in Malaysia),

I have a source code of setting the textbox for key in Numeric only. But my problem is why i can't backspace? Can anyone help me please, thanks.

Expand|Select|Wrap|Line Numbers
  1. Private Sub txtProDate_KeyPress(KeyAscii As Integer)
  2.    If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
  3.       KeyAscii = 0
  4.       Beep            ' Sound error signal.
  5.    End If
  6. End
May 17 '07 #1
5 9783
cmrhema
375 256MB
Good morning (in Malaysia),

I have a source code of setting the textbox for key in Numeric only. But my problem is why i can't backspace? Can anyone help me please, thanks.

Expand|Select|Wrap|Line Numbers
  1. Private Sub txtProDate_KeyPress(KeyAscii As Integer)
  2.    If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
  3.       KeyAscii = 0
  4.       Beep            ' Sound error signal.
  5.    End If
  6. End
The ascii code for backspace is 8, which in your code gets trapped.
Refer the links below
http://www.thescripts.com/forum/thre...-keyascii.html
May 17 '07 #2
wassup
34
The ascii code for backspace is 8, which in your code gets trapped.
Refer the links below
http://www.thescripts.com/forum/thread620015-keyascii.html
Thanks cmrhema.
Here the code after revise set the textbox to Numeric and allow for Backspace.

Expand|Select|Wrap|Line Numbers
  1. Private Sub txtProDate_KeyPress(KeyAscii As Integer)
  2.    If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
  3.       KeyAscii = 8
  4.       Beep            ' Sound error signal.
  5.    End If
  6. End Sub
May 17 '07 #3
Killer42
8,435 Expert 8TB
I think you might see some quite interesting and unexpected results from that code. This version might work slightly better...
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtProDate_KeyPress(KeyAscii As Integer)
  2.   Select Case KeyAscii
  3.     Case Is < 32
  4.       ' Ignore
  5.     Case Asc("0") To Asc("9")
  6.       ' Ignore
  7.     Case Else
  8.       KeyAscii = 0
  9.       Beep            ' Sound error signal.
  10.   End Select
  11. End Sub
May 17 '07 #4
wassup
34
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtProDate_KeyPress(KeyAscii As Integer)
  2.   Select Case KeyAscii
  3.     Case Is < 32
  4.       ' Ignore
  5.     Case Asc("0") To Asc("9")
  6.       ' Ignore
  7.     Case Else
  8.       KeyAscii = 0
  9.       Beep            ' Sound error signal.
  10.   End Select
  11. End Sub
Hi killer,
I still not very understand what the diffrent of this two source code. Why this more better? Can you teach me more detail? Thanks.
May 17 '07 #5
Killer42
8,435 Expert 8TB
Well if you follow the original code step by step, it effectively says "if the user pressed anything other than a number, then change it to a backspace". This is not very useful.

I tried it out. What happens (as expected) is that you can type numbers, but every time you press a lett, or anything else, it just erases the last digit.

The modified version I posted says something like this...
  • If user pressed some sort of control key (arrows, enter, whatever) then leave it alone.
  • If user pressed a numeric digit, then leave it alone.
  • If user pressed anything else, don't allow it to go through (suppress it).
May 18 '07 #6

Sign in to post your reply or Sign up for a free account.

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...
0
by: Mark Johnson | last post by:
The last reply got sort of cutoff. So here again: So for anyone interested, here's the simple regexp patterns for the substitutions required. The textbox control is being 'zoomed' in a popup...
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...
4
by: kidzero | last post by:
Hello, i have text box control in from(windows form). Now i want that this textbox could accept only numeric values. In MFC editbox had special option (number) and everything was well, but in C#...
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...
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...
1
by: D. Shane Fowlkes | last post by:
I have a asp:textbox which I've applied a javascript function to strip off all non-numeric chanarcters. The js function is triggered with the textbox's onblur attribute. I also have a...
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)
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
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?
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
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...
0
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...
0
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,...
0
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...

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.