Use one routine for several events

  • Thread starter Thread starter Alain Dekker
  • Start date Start date
A

Alain Dekker

I'd like to set a flag called "bEditing" (Dim as Boolean) whenever the user
has entered (called the Enter event) on any of the edit boxes on my form.
Similarly I'd like to set "bEditing" to False when the Leave event is called
on any edit box.

Currently I'm having to set a separate sub-routine for each event for each
edit box. Is there a way to pool them? You could do with Visual C++ 6.0 by
manually editing the message map, and most other languages so I expect
there's away to do it in VB!

Using VB 2003.NET

Thanks!
Alain
 
Alain said:
I'd like to set a flag called "bEditing" (Dim as Boolean) whenever the user
has entered (called the Enter event) on any of the edit boxes on my form.
Similarly I'd like to set "bEditing" to False when the Leave event is called
on any edit box.

Currently I'm having to set a separate sub-routine for each event for each
edit box. Is there a way to pool them? You could do with Visual C++ 6.0 by
manually editing the message map, and most other languages so I expect
there's away to do it in VB!

Using VB 2003.NET

You can have the same procedure handle the event of all boxes, either by
executing Addhandler for every box, or by naming them all with the Handles
clause:

sub bla(...) handles edit1.leave, edit2.leave, edit3.leave
end sub
 
Try this:

Imports System
Imports ...
...

Dim arrTxt() As TextBox, bEditing As Boolean

Private Sub Form1_Load(...) Handles MyBase.Load
...
arrTxt = New TextBox(){txt1, txt2, txt3, ...}
For Each txt As TextBox in arrTxt
AddHandler txt.Enter, AddressOf txt_Enter
AddHandler txt.Leave, AddressOf txt_Leave
Next
...
End Sub

Private Sub txt_Enter()
bEditing = True
End Sub

Private Sub txt_Leave()
bEditing = False
End Sub


Rich
 
Thanks very much to you both! That was very helpful and worked exactly as
you said...

Alain
 
Back
Top