Newbie Question; How do i add an eventhandler to all my textfields?

  • Thread starter Thread starter Dan Keeley
  • Start date Start date
D

Dan Keeley

Hi,

I want to do this:

AddHandler txtSupplierName.KeyPress, AddressOf EnterHandler

To all my txtXX textbox fields??

I have an inkling i'll have to define my own textBox class, is that right?
Any pointers?

Rgds,
Dan
 
Hi Dan,

No need to create your own class. Try this:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim txt As TextBox
For Each txt In Controls
AddHandler txt.KeyPress, AddressOf EnterHandler
Next
End Sub

Private Sub EnterHandler(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)
' EnterHandler() code.
End Sub

Does this help?

Take care,

Eric
 
No need to create your own class. Try this:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim txt As TextBox
For Each txt In Controls
AddHandler txt.KeyPress, AddressOf EnterHandler
Next
End Sub

Private Sub EnterHandler(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)
' EnterHandler() code.
End Sub

Does this help?

Fantastic that'll do it!

Thanks loads!
Dan
 
* "Eric Lemmon said:
No need to create your own class. Try this:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim txt As TextBox
For Each txt In Controls

This will fail.

\\\
Dim ctr As Control
For Each ctr In Me.Controls
AddHandler DirectCast(ctr, TextBox).KeyPress, ...
Next ctr
///
 
* "Dan Keeley said:
AddHandler txtSupplierName.KeyPress, AddressOf EnterHandler

To all my txtXX textbox fields??

\\\
Private Sub Form1_ControlAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles MyBase.ControlAdded
If TypeOf e.Control Is TextBox Then
AddHandler DirectCast(e.Control, TextBox).KeyPress, AddressOf Me.TextBox_KeyPress
End If
End Sub

Private Sub Form1_ControlRemoved(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles MyBase.ControlRemoved
If TypeOf e.Control Is TextBox Then
RemoveHandler DirectCast(e.Control, TextBox).KeyPress, AddressOf Me.TextBox_KeyPress
End If
End Sub

Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
MsgBox(e.KeyChar)
End Sub
///
 
\\\
Private Sub Form1_ControlAdded(ByVal sender As Object, ByVal e As
System.Windows.Forms.ControlEventArgs) Handles MyBase.ControlAdded
If TypeOf e.Control Is TextBox Then
AddHandler DirectCast(e.Control, TextBox).KeyPress, AddressOf Me.TextBox_KeyPress
End If
End Sub

Ok neither of these suggestions work, however thats due to my screen, and
something i'm missing...

As it loops through i see 3 controls and 3 only - my listbox, my card dialog
and what i assume is my other group box.

It doesnt loop through the fields within the card dialog?? Is that
expected? how can i make it do that?

Thanks!

Rgds,
Dan
 
* "Dan Keeley said:
It doesnt loop through the fields within the card dialog?? Is that
expected? how can i make it do that?

If you are using a loop, you will have to enumerate recursively (untested):

\\\
Private Sub EnumerateControls( _
ByVal CurrentControl As Control _
)
Dim ctr As Control
For Each ctr In CurrentControl.Controls
If TypeOf ctr Is TextBox Then
AddHandler DirectCast(ctr, TextBox).KeyPresse, ...
End If
EnumerateControls(ctr)
Next ctr
End Sub
///
 
My apologies, Dan. I typed that one up a little too quickly. Here's the
correct code to place in the form's load event:

Dim ctl As Control
For Each ctl In Controls
If TypeOf ctl Is TextBox Then
AddHandler DirectCast(ctl, TextBox).KeyPress, AddressOf EnterHandler
End If
Next

Regards,
Eric
 
Hi Herfried,

Are you sure there is enough memory.
:-))

To be serious it needs a start which looks for newbies a litte bit strange.
EnumerateControls(me)

Cor
 
* "Cor Ligthert said:
Are you sure there is enough memory.
:-))
LOL!

To be serious it needs a start which looks for newbies a litte bit strange.
EnumerateControls(me)

Yep, forgot to add that. Thank you!
 
Dan,
I have an inkling i'll have to define my own textBox class, is that right?
Any pointers?
In addition to the brute force method of using AddHandler that the others
discussed, a couple of other possibilities include:

1) you can manually add each handler using the Handles syntax.

Private Sub TextBox_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles TextBox1.KeyPress, TextBox2.KeyPress, _
TextBox3.KeyPress, TextBox4.KeyPress
MsgBox(e.KeyChar)
End Sub

2) you can derive your own TextBox class and encapsulate all the events
(subs, functions, properties) in a single class (which is not your form
class).

Public Class TextBoxEx
Inherits TextBox

Protected Overrides Sub OnKeyPress(ByVal e As
System.Windows.Forms.KeyPressEventArgs)
MyBase.OnKeyPress(e)
' Your logic here
End Sub

End Class

I will use the first above if I only have a handful of text boxes. I will
use the second above if I have multiple events to handle in addition to
needing to have additional information (properties) associated with each
TextBox. Or I need to handle the same stuff on multiple forms...

I will use the brute force method, if I only have a single event that I need
to handle on a single form (on multiple text boxes), which is rare...

The biggest problem with the second method above, is getting the control to
appear on the tool box so its easy to use. I normally manually change the
source to use my textbox over the normal text box...

Hope this helps
Jay
 
Back
Top