Creating a generic event handler for a form ?

A

AlexT

Folks

I'm trying to create a generic event handler for textboxes on a form.
The concept would be to consolidate event handling at one place.

To test my idea I have created the following class module

-----------------------------------------------------------------------------------
Option Compare Database
Option Explicit

Public WithEvents xTxtBox As TextBox


Private Sub xTxtBox_Click()
MsgBox "click from " & Me.xTxtBox.name
End Sub

Private Sub xTxtBox_DblClick(Cancel As Integer)
MsgBox "2x click from " & Me.xTxtBox.name
End Sub
-----------------------------------------------------------------------------------

Then, in my form LOAD event I have inserted

-----------------------------------------------------------------------------------
txtCount = 0
' Setup textbox handlers
For Each ctl In Me.lstObj.Controls
If ctl.ControlType = acTextBox Then
txtCount = txtCount + 1
ReDim Preserve txtControls(1 To txtCount)
Set txtControls(txtCount).xTxtBox = ctl
End If
Next ctl

txtControls being defined within the form as

Private txtControls() As New clsControl
-----------------------------------------------------------------------------------

Now this executes fine... but the click / dblClick events are never
raised in my class module...

Any idea / suggestion most welcome


--alexT
 
D

Douglas J Steele

Sorry, no time to test, but I believe you need to declare your array as
WithEvents:

Private WithEvents txtControls() As clsControl
 
A

AlexT

Well,

it seems that it DOES work... when there is an event handler for that
specific event within the form.

As far as I can see the form's event handler is fired, then the custom
one.

Is that the expected behaviour ?

Thanks & regards

--alexT
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top