|
-
Sep 8th, 2011, 11:24 AM
#1
Thread Starter
Member
[RESOLVED] Call a keypress method dynamically for each control in vb.net 2005
I working on a project that includes to call a certain type of method to each control, i have this code:
Code:
Private Sub txtBcNum1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtBcNum1.KeyPress
If Char.IsDigit(e.KeyChar) Or e.KeyChar = Chr(8) Then
e.Handled = False
Else
e.Handled = True
End If
End Sub
This code works like a charm if i want to allow only numbers and backspace on my textbox.
Problem: I have 15 textboxes( txtBcNum1,txtBcNum2,....,txtBcNum15 ), what's the best way to call this function inside KeyPress method on each textboxes with out manually adding it to KeyPress method?
-
Sep 8th, 2011, 11:29 AM
#2
Re: Call a keypress method dynamically for each control in vb.net 2005
remove the handles keyword part of the sub definition, then on the form load, loop through all the text boxes in the controls collection and use the AddHandler to add the handler to the KEyPress event of each textbox.
-tg
-
Sep 8th, 2011, 11:36 AM
#3
Thread Starter
Member
Re: Call a keypress method dynamically for each control in vb.net 2005
 Originally Posted by techgnome
remove the handles keyword part of the sub definition, then on the form load, loop through all the text boxes in the controls collection and use the AddHandler to add the handler to the KEyPress event of each textbox.
-tg
Thanks for the reply. That was my idea also but I'm having problem constructing the actual code. If you have a pseudo code that i can reference that will be very helpful.
-
Sep 8th, 2011, 11:58 AM
#4
Re: Call a keypress method dynamically for each control in vb.net 2005
something like this:
Code:
for each ctrl text box in form controls
addhandler ctrl keypress, address of event handler
loop until no more text boxes
-tg
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|