numeric textbox help

D

dzemo

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
 
Y

Yalcomania

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
 
S

Steve Cutting

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
 
C

Cor Ligthert

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
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top