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

numeric textbox help

hi i have tis procedure for my text box

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Char.IsDigit(e.KeyChar) Then
e.Handled = False
Else
e.Handled = True
End If
End Sub

and work fine but i cant use backspace to delete number if i mistake or cant
use "," or "." for decimal numbers. Any sugestion? Please help
Thanks
Nov 20 '05 #1
4 1924
I think this helps... I don't know maybe there is a better way to do that,
but i used similar code in vb6....

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Char.IsDigit(e.KeyChar) Or Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) =
44 Or Asc(e.KeyChar) = 46 Then
e.Handled = False
Else
e.Handled = True
End If
End Sub
Nov 20 '05 #2
Here are a few examples
http://www.wizkil-webs.net/NET/DotNe...m#_Toc78169276

and

http://www.wizkil-webs.net/NET/DotNe...m#_Toc78341567

B
"dzemo" <dz***@wizard.ba> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
hi i have tis procedure for my text box

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Char.IsDigit(e.KeyChar) Then
e.Handled = False
Else
e.Handled = True
End If
End Sub

and work fine but i cant use backspace to delete number if i mistake or cant use "," or "." for decimal numbers. Any sugestion? Please help
Thanks


Nov 20 '05 #3
I wrote this simple general-purpose key-validating function. You might find
it useful. just call it like this in your keypress event:

If Not ValidateKey(Asc(e.KeyChar), "mask") Then e.Handled = True

"mask" contains pairs of characters. The function checks that your keypress
falls between the ascii range of each pair. For instance if you want to
allow only lowercase letters just use "az". If you want only numbers and
decimal points use "09.." Include as many pairs as you like.

hope this helps
Steve

Function ValidateKey(ByVal Key As Integer, ByVal KeyMask As String) As
Boolean

'PURPOSE: check that a keystroke was allowed by key mask. return
true/false

'declare variable
Dim intCharCheck As Integer = 0

'assume invalid
ValidateKey = False

Do Until intCharCheck = KeyMask.Length Or ValidateKey = True

'check if keypress falls between pair of characters in KeyMask
If Key >= Asc(KeyMask.Chars(intCharCheck)) And Key <=
Asc(KeyMask.Chars(intCharCheck + 1)) Then ValidateKey = True

'advance to next pair in KeyMask
intCharCheck += 2

Loop

'allow backspace
If Key = Keys.Back Then ValidateKey = True

End Function

Nov 20 '05 #4
Hi Dzemo,

See this sample I once made
(The last part is to prevent that people past in wrong things)

I hope this helps?

Cor

\\\
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.TextBox1.MaxLength = 10
End Sub
Private Sub textbox1_KeyUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles textbox1.KeyUp
If e.KeyValue <> 8 Then
If Not IsNumeric(TextBox1.Text) Then
If TextBox1.Text.Length > 0 Then
MessageBox.Show("Only numeric is allowed")

TextBox1.SelectionStart = TextBox1.Text.Length - 1
TextBox1.SelectionLength = 1
End If

End If
End If
End Sub
Private Sub TextBox1_Validating(ByVal sender _
As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
Handles TextBox1.Validating
If TextBox1.Text.Length > 0 Then
If Not IsNumeric(TextBox1.Text) Then
MessageBox.Show("There was an error pasting")
TextBox1.Focus()
e.Cancel = True
End If
End If
End Sub
///
Nov 20 '05 #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: 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...
7
by: BBFrost | last post by:
I'm receiving decimal values from database queries and placing them on a report page. The users want to see the following .... Db Value Display Value 123.3400 123.34...
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,...
0
by: EgoSum | last post by:
Can someone help me with custom text box? I want change behavior custom date text box - disallow entry and pass entry from numeric keyboard to a text box. Code below disallow entry, but how I can...
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...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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,...
0
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...

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.