Advanced Topic: Can Events "Bubble Up" to the Form

Greg Truby

MrExcel MVP
Joined
Jun 19, 2002
Messages
10,025
Tried searching the archives to no avail. Here's the deal:

Userform, lots of controls. Have a button that when clicked, will make a call to terminal emulation software and pull back information and drop that information into textbox controls. The series of textboxes varies depending on which row of controls has focus (twenty-four rows of controls visible at a time). The button works fine when clicked because TakeFocusOnClick is set to FALSE. However, if I assign an accelerator key to the button, the accelerator sets the focus to the button and now I don't know which row of textboxes should be the target of the information gathered by the button's subroutine because they no longer have focus.

My thought was to create a global object variable to hold a pointer to the control that had focus prior to the button taking focus.

The controls that are in the same row as the target textboxes and hence can have focus prior to the button's method firing can be a mix of ComboBoxes, Textboxes or RefEdits. So trying to dump all of these into an array + class module type of solution is a not an attractive solution (if it's even possible).

So what I'm thinking is to try and trap on KeyDown events (to trap tabbing from control to control) and on MouseDown events, but at a form level to see if I can set up a way to track using a global object variable like I mentioned above. But when I put simple tests in the UserForm's code like:

Code:
Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    MsgBox "Form Keydown"
End Sub

Private Sub UserForm_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    MsgBox "Form MouseDown"
End Sub

they are not firing. So I'm assuming that the KeyDown and MouseDown events are getting handled at a lower level and not bubbling up to the parents (Page|MultiPage|UserForm). So now we're to the title of this thread and my question: Is there some type of property setting or something I can do to get the KeyDown and MouseDown events to bubble up the chain of containers and hit the form's event handlers?

Another idea that occurs to me is to use a class module and try to assign the userform to that WithEvents. Is that a possibility?

Or if anyone has a brilliant idea on another way to attack this problem, I'm open to suggestions. :p
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Re: Advanced Topic: Can Events "Bubble Up" to the

Just an addendum - trying to put a UserForm With Events inside a separate class module ain't working too well either, i.e.

(Class Module)
<font face=Courier New><SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">WithEvents</SPAN> ufUserForm <SPAN style="color:#00007F">As</SPAN> UserForm

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Class_Initialize()
    Application.StatusBar = "UF Class Init"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> ufUserForm_KeyDown(<SPAN style="color:#00007F">ByVal</SPAN> KeyCode <SPAN style="color:#00007F">As</SPAN> MSForms.ReturnInteger, <SPAN style="color:#00007F">ByVal</SPAN> Shift <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>)
    MsgBox "Class Form KeyDown"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> ufUserForm_MouseDown(<SPAN style="color:#00007F">ByVal</SPAN> Button <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> Shift <SPAN style="color:#00007F">As</SPAN> Integer, <SPAN style="color:#00007F">ByVal</SPAN> X <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Single</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> Y <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Single</SPAN>)
    MsgBox "Class Form MouseDown"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

(Standard Module)
<font face=Courier New>    <SPAN style="color:#00007F">Set</SPAN> formDataScraper.ufUserForm = ufXL_DataScraper
    ufXL_DataScraper.Show FormShowConstants.vbModal</FONT>

Is not trapping any KeyDown or MouseDown events either.
 
Upvote 0
Re: Advanced Topic: Can Events "Bubble Up" to the

Just an idea. Text boxes have an Enter event. Perhaps you could have a global variable "LastBox" and reset this each time the user selects it. eg.

Private Sub TextBox1_Enter()
LatTextBox=1
End Sub
 
Upvote 0
Re: Advanced Topic: Can Events "Bubble Up" to the

Brian,

Thank you for taking the time to read my post and offer a response. That solution will, of course, work. But that's what I'm trying to avoid. Since I'm trying to ride herd on 300 (that's the actual count) different textboxes, comboboxes and refedits, I'm trying to suss out a solution that doesn't require adding 300 event handling procedures into the form's code module. Unfortunately, I'm meeting dead ends on all other paths, so that may indeed be what I end up doing (thank goodness for the VBIDE library and the ability to create those 300 event handlers programmatically, if it comes to that).

Normally one can create a class module and put a generic variable WithEvents in the class module and then declare an array of that class module; setting the control variable for each array element to the control on the form and thereby create a "group event handler". But for some blasted reason the menu of events that the control object variables inside class modules handle is not the complete event menu. Textbox controls inside a class module do not support the standard Enter and Exit events, so this approach hits a wall. Which is why I had hoped to find some means of using an event handler at the form level to solve the problem.

Again, thanks for trying.

Regards,
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,429
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top