I have a VB6 application where I'd like to have a consistent behavior among its controls application-wide. One of the behaviors, for example, would be highlighting a text box when it gains focus and removing the highlight when it loses focus. I'd like this to happen on every form.
What I'm trying to do is have one sub procedure that can be called by all forms when they load that will make this behavior happen. That way, I don't have to manually code for each individual text box to make it highlight.
I've tried getting VB6 to attach an event handler to a control at runtime but it just barks at me. I come from a .Net background so maybe I'm approaching it wrong for VB6. But how can I get this desired behavior without having to manually code it for every control?
You could also "Subclass" Your TextBox Controls Using WithEvents
. The advantage here is that you can code the highlighting and de-highlighting in one place without having to go through and replace all of your existing controls (as Scott suggests).
The downside is that you have to add code to the Form_Load event of all your forms to "register" the controls on that form. However, even this should not be too bad if you want to apply the technique to every control; in that case, you just need to write a function that loops through the .Controls
collection of a form and registers each control. Then just call this function in each form's Form_Load event.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With