Universal function to check for entered digits only

J

John Smith

Hello,

I'd like to create a function that will allow users to only enter
numbers in some textboxes. I'm trying to create only one function and
call it for each textbox.keypress event instead of creating one for
each textbox.

This is what I have (and works):

Private Function DigitsOnly(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles _
txtOrderNumber.KeyPress

' set the Order Number textbox to accept only digits, the
Backspace, the Tab, or the Enter key.
If Not (Char.IsNumber(e.KeyChar) OrElse AscW(e.KeyChar) = 8
OrElse AscW(e.KeyChar) = 9 OrElse AscW(e.KeyChar) = 13) Then
e.Handled = True
End If

End Function

However, how do I create it without stating the textbox handler
(Handles txtOrderNumber.KeyPress) so I can call it from any textbox,
pass the textbox name as the object to be handled and then execute it?

For example,

' call the DigitsOnly function
DigitsOnly(txtMyOtherTextBox.name)


Right now if I try to create a variable for this and use it I get an
error stating "Handles clause requires a WithEvents variable).

Thanks for all your help guys (and gals)!

J
 
G

Guest

I'd like to create a function that will allow users to only enter
numbers in some textboxes. I'm trying to create only one function and
call it for each textbox.keypress event instead of creating one for
each textbox.

How about creating a custom order number textbox?
 

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