By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,639 Members | 1,326 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,639 IT Pros & Developers. It's quick & easy.

numeric textbox help

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.