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?
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?