Mousedown event for frame control does not always fire

JohnM77

New Member
Joined
Feb 4, 2016
Messages
1
Hoping someone can help with this - I am trying to develop a class for userform textbox validation using events. I would like to prevent the user from exiting the textbox if the textbox text is not valid (such as blank or non-numeric value, etc.) for whatever reason. This would normally be done in the textbox exit event (using the Cancel argument), but the lack of the exit event for a class that contains a textbox defined WithEvents is well documented, so I am trying to take a different approach.

Instead of preventing the focus from leaving the offending textbox, I am trying to detect movement of the focus to a new control and then setting the focus back to the textbox. I am making progress, but have encountered a problem with the frame control. Here is a simple case - assume Textbox1 is placed directly on the form and Textbox2 is located inside a frame control. If TextBox1's text is invalid (blank for this simple example), I would like to set the focus back to it when the user clicks on the frame control. So I put code in the frame's MouseDown event to set the focus back to TextBox1 if it's text is blank.

This works well (focus returns to TextBox 1) if I click in an area that is clearly inside the frame control. However, if I click just inside the frame border or on the frame caption area, the focus sometimes moves to the frame control. I determined that this occurred because the MouseDown event did not occur in these cases. I found MouseUp had the same problem.

But then I discovered that using the frame's Enter event produced the desired result - focus always bounced back to TextBox1, no matter where I click on the frame control. This is fine, except that the frame's Enter event code must be done at design time, since the enter event is also not available for a frame declared WithEvents in a class. This is annoying and I would like to be able to use MouseDown instead, but cannot solve this problem. I get the same result in Excel 2003, 2010, and 2013. Does anyone have any idea why the MouseDown and MouseUp events are sometimes missing as I described?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Forum statistics

Threads
1,223,155
Messages
6,170,401
Members
452,324
Latest member
stuart1980

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