Keypress event

K

Kay

Hi all,

I created a function to limit only number(dot/del etc are ok) can be entered
into a text box. If an invalid character is entered I simply want to cancel
what is entered - this part works but, if a VALID character is entered it
produce "Specified cast is not valid"... can you tell why? Here's my code:
Function OnlyNumber(ByRef e As System.Windows.Forms.KeyPressEventArgs)
As Boolean
Dim iAsc As Integer

e.Handled = False
iAsc = Asc(e.KeyChar)

Select Case iAsc
Case 8
'user press backspace, do nothing
Case 127
'user press del, do nothing
Case 48 To 57
'valid key, do nothing from 0-9
Case 44
'valid key, comma
Case 46
'valid key, dot
Case Else
'other invalid key
OnlyNumber = True
End Select


End Function
<<<<<<<

Thanks!

Kay
 
K

Kay

Hi all,

I found the problem now which is not directly caused by the function, indeed
it has conflict with the text changed event... :p

Thanks for your time to read my post!!
 
C

Cor Ligthert [MVP]

Kay,

Try for this kind of functions the KeyUp event, it gives you much more
information.

Cor
 
K

Kay

Oh!!! Cool Cor, thanks! :D

Kay said:
Hi all,

I created a function to limit only number(dot/del etc are ok) can be
entered into a text box. If an invalid character is entered I simply want
to cancel what is entered - this part works but, if a VALID character is
entered it produce "Specified cast is not valid"... can you tell why?
Here's my code:

Function OnlyNumber(ByRef e As System.Windows.Forms.KeyPressEventArgs)
As Boolean
Dim iAsc As Integer

e.Handled = False
iAsc = Asc(e.KeyChar)

Select Case iAsc
Case 8
'user press backspace, do nothing
Case 127
'user press del, do nothing
Case 48 To 57
'valid key, do nothing from 0-9
Case 44
'valid key, comma
Case 46
'valid key, dot
Case Else
'other invalid key
OnlyNumber = True
End Select


End Function
<<<<<<<

Thanks!

Kay
 

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

Similar Threads


Top