I use
Private Sub Number_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles txtAmount.KeyPress, txtOrccNumber.KeyPress
Select Case e.KeyChar
Case Convert.ToChar(8) ' Backspace
Case "0" To "9"
Case Else
e.Handled = True
End Select
End Sub
Change the handles clause to include your control(s).
That is a good solution. But you may still find users who will copy text and paste it into your textbox.
If that is a concern, then I wouldn't prevent the user from doing so.
What I would do is check the values of your Textbox(es) before you process your function which commits the values. Use the IsNumeric(TextBox1.Text) function.
For example, don't process your save or whatever you have going on if there are errors, and indicate to the user which values are inacurrate.
Thanks , I was looking for the mentioned code , I already done that but I was unable to create logic for Backspace and you solved my problem. Right now , I am checking IsNumeric before calling my save functions.
Thanks.
Muhammad Essa Mughal
Software Engineer
iLogic Inc.
You can put that IsNumeric in the TextChanged event of your textbox as well. If the user copies/pastes text into the textbox, the changed event will fire and you can catch it there as well.
I read that FAQ and I am trying to use it but I am wondering either I have to make user control or how can I use that class for any text box. Could you please explain how can I use it.
Right now , I am using JohnYingling's suggested code and it works fine and I added two more cases for "." and "-" in that code.
Thanks for your help.
Muhammad Essa Mughal
Software Engineer
iLogic Inc.
in the textboxes "textchanged" event you could simply run a validation of the text (just make a function to loop through every char and if 1 or more chars is not a # or a -then make it return a 0, or if its all good a 1)
---------------------------------------
HERES WHAT THE CODE WOULD BASICALLY BE:
---------------------------------------
Dim previoustxt as string = "" 'make this at the topmost part just after the class is declared
Private Sub textbox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox.TextChanged
if validatetxt(textbox.text) = 0 then textbox.text = previoustxt
previoustxt = textbox.text
End Sub
Private Function validatetxt(ByVal str As String) As Integer
Dim x
For x = 0 To Len(str) - 1
validatetxt = 0
If Char.IsDigit(str.Chars(x)) = True or mid(str, x + 1, 1) = "-" Then validatetxt = 1
If validatetxt = 0 Then Exit For
Next
End Function
that would prevent invalid text from being pasted in, i just gave you this code quick so if there are any problems just tell me. hope this helps
Thanks for giving me nice suggestions and now I got it ! I'll take care of Cut/copy and Paste functoin as well.
Now, I am using this function !
Public Function AllowNumeric(ByVal e As Object, Optional ByVal IsDecimalAllow As Boolean = True, Optional ByVal IsMinusSignAllow As Boolean = False)
Select Case e.KeyChar
Case Convert.ToChar(8) ' Backspace
Case "0" To "9"
Case IIf(IsDecimalAllow = True, ".", "")
Case IIf(IsMinusSignAllow = True, "-", "")
Case Else
e.Handled = True
End Select
End Function
I appreciate all the help which you all gave to me.
Thanks.
Muhammad Essa Mughal
Software Engineer
iLogic Inc.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.