Add an event handler to each control on a form at runtime VB6

11,284

Solution 1

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.

Solution 2

Check this out:

Control Arrays for Visual Basic 6.0 Users

Solution 3

Unfortunately VB6 does not support implementation inheritance and you can't inherit TextBox and just modify or add functionality. It does not support COM aggregation too, though I doubt ActiveX controls specification supports it too.

What you are left with is reimplementing a control from scratch or implementing a custom UserControl that contains the original one and forwards every method, property or event. The problem with the latter approach is not that it's lots of pointless code but the performance of VB6's custom user controls. Built-in controls are really fast and you can place hundreds of labels or textboxes before noticing degradation.

What I'm doing in cases like yours is to implement an extender class that holds a reference to the textbox control, subclasses it and/or listens and responds to raised events from the control. The extender class implements the desired/modified behavior on GetFocus event or WM_GETFOCUS, whatever. Next, for each textbox on the form an instance of the extender is initialized with a reference to the control. All the extenders are held in a collection which can be part of a class that extends the form itself. The form extender can wrap the instantiation and initialization of the control extenders (the For Each In Controls part).

I'm doing this constantly, having very rich extenders for every possible control I'm placing on forms that wrap every property/method I'm accessing. I'm listening for events only on the extenders too. The nice part is that when I find a bug in a 3rd party control I can mitigate it very easily in the control extender.

Solution 4

Another way to achieve the behaviour you want is not to handle the textbox events at all. Instead, set up a Timer control with a small tick interval, say 50 milliseconds. In the Tick event, check Me.ActiveControl to see whether the focus has moved, and highlight/dehighlight accordingly. You will need a static variable to remember which control has the focus.

This is a nice easy way to get a universal GotFocus / LostFocus event handler in VB6.

Solution 5

The appropriate way to do what you're asking is to define a new UserControl (MyAdvancedTextBox) and code your intended behavior in there. Then replace all of your text boxes with that user control. It's a lot of work, but it's less work than the alternative:

Manually define an event handler in the code-behind for each text box (or text box control array) and have the event handler pass itself to some common module subroutine that executes your common handling logic.

VB6 events are a lot more primitive than .NET.

Share:
11,284
John
Author by

John

I'm an Application Developer in Long Island, NY.

Updated on June 14, 2022

Comments

  • John
    John about 2 years

    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?