Hi, I am writing a custom VBA macro which pops up a UserForm.
In this user form there are aprox. 20 text boxes which I create
programatically. Since there are a variable number of text boxes
created based on the state of the underlying worksheet, in order for
events to work for the text boxes I needed to create a class as
MsForms.TextBox. I have the following code (this is a snippit of what
I have, I have ommited the non-relavant code):
Public WithEvents MyTextBox As MSForms.TextBox
<non-relavant code here>
Private Sub MyTextBox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
ByVal Shif As Integer)
'This event DOES work!
<non-relavant code here>
end sub
Private Sub MyTextBox_Enter()
'this event does NOT work
MsgBox "I've made it here"
end sub
Private Sub MyTextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'This event does NOT work
Msgbox "I've made it here also"
end sub
The above code is all within a class module named MyTextBox. I use the
following code in a module to create the ~20 instances of the text
boxes. This all works properly as all 20 text boxes are created, and
keyboard (key down & up) events are passed, and handled properly, into
the event handler of the MyTextBox class (as illustrated in the code
above).
The following code is in a Module, and is used to create the text
boxes, and populates an array of MyTextBox's in order to keep
reference to those text boxes:
Set textboxarray(x).MyTextBox = CategoryForm.category.Controls.Add_
("Forms.TextBox.1", "txtbox", True)
textboxarray(x).MyTextBox.Height = textBoxHeight
textboxarray(x).MyTextBox.Width = textBoxWidth
textboxarray(x).MyTextBox.left = textBoxPositionLeft
textboxarray(x).MyTextBox.top = positionTop
textboxarray(x).MyTextBox.Name = "txtbox" & x
The above code (as you probably guessed) is in within a loop which
populates the textboxarray, the textboxarray is initialized globaly
as:
Dim textboxarray() As New MyTextBox
The problem, and question I now have is simple. Why, or more
importantly how can I receive the Enter/Exit events in my class. Other
events work in my class, but not the enter/exit events (when focus is
given and taken from the MyTextBox control). I have created a standard
text box and the enter/exit events work for that text box fine, but I
can't figure out why I don't get enter/exit events for the MyTextBox
class. Also in the VBA editor in excel, it has a pulldown list of all
the events when I'm editing the MyTextBox class. it seems to list all
events except for the Enter/Exit events. The excel VBA documentation
illustrates the use of the Enter/Exit events for a text box, and I use
it as illustrated in the documentation. Just it doesn't work with my
class.
Thank you,
David |