[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?
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
Re: Call a keypress method dynamically for each control in vb.net 2005
Quote:
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.
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