Is there a way to write a class that will listen into all the events that take place on a form, including the events from the controls on the form?
What I want to do is listen for all the GotFocus events that take place and then, depending on the control type, invoke some code. I thought that if this was possible it would be better then having to instantiate a new class for every control that I want to listen into on the form's Load.
I was going to write a class that would move a colored box on the form to indicate where the focus was. I only wanted it to highlight textboxes and combo boxes. The thought was that if you could listen into all the events on the form you could then respond to only the ones that you care about.
Otherwise, you would have to setup the events for controls that you want to listen on. I was trying to make it very generic and reusable. I don't want to setup the events for each control when I design the form. (Lazy I know but work smarter not harder!).
If you have any other ideas or directions let me know. Thanks.
ActiveControl will tell me which control I would need to move the box to but how do I know when to move the box. That is why I wanted to listen to events. When a GotFocus event was fired I would check the control and move the box to the appropriate control.
Well this is what I came up with (with help from a post by Bob Bedell).
Create a class named clsTextBox with the following code:
Option Compare Database
Private m_Box As Rectangle
Private WithEvents m_TextBox As TextBox
'Setup the new class
Public Sub Initialize(TBox As TextBox, Box As Rectangle)
Set m_TextBox = TBox
m_TextBox.OnGotFocus = "[Event Procedure]"
m_TextBox.OnLostFocus = "[Event Procedure]"
Set m_Box = Box
'Clean up trash when done
Private Sub Class_Terminate()
Set m_TextBox = Nothing
Set m_Box = Nothing
Private Sub m_TextBox_GotFocus()
'show box and move around the text box
m_Box.Visible = True
.Left = m_TextBox.Left - 100
.Top = m_TextBox.Top - 100
.Width = m_TextBox.Width + 200
.Height = m_TextBox.Height + 200
Private Sub m_TextBox_LostFocus()
'hide the box when the text box looses focus
m_Box.Visible = False
Then create a form with a few TextBoxes and a rectangle that will be used to highlight the textbox and insert the following code:
Private colTextBoxes As New Collection 'stores the instances of clsTextBox
Private ctlTextBox As clsTextBox
Private Sub Form_Load()
Dim ctl As Control
Dim i As Integer
'loop through all the text boxes on the form and instantiate
'the class and add to collection
For Each ctl In Me.Controls
If .ControlType = acTextBox Then
Set ctlTextBox = New clsTextBox
ctlTextBox.Initialize ctl, Box
Now you have a box that will highlight the textbox on the form that has the focus. And you don't have to write a ton of code for each individual TextBox on the form.
Seriously, is it worth it? You can't just get users to understand the flashing cursor concept?
Another way you might consider doing this is just having your text / combo boxes have a transparent background.... then when the focus arrives at one of them, the backcolour will show. If you set your backcolour to red or something, it would be pretty difficult to not know which field has the focus.