foundationcros.blogg.se

Assign keyup to multiple textboxes
Assign keyup to multiple textboxes








assign keyup to multiple textboxes
  1. Assign keyup to multiple textboxes how to#
  2. Assign keyup to multiple textboxes code#

The Select statement is there as I haven't worked out how to handle every single event yet - only events that don't need arguments work with this code, so things like mouse events are currently out.( KeyPress is a special case - by turning Key Preview on at the form level, I can capture the KeyAscii required into mLastKeyPressedAscii in the form's Form_KeyPress event, and pass that) The error handling stops it crashing if TextBox0_GotFocus() doesn't exist, removing the necessity for having empty handlers for every single control. For example, if it's handed TextBox0 and GotFocus, it will attempt to call TextBox0_GotFocus(). This attempts to call a method named with the standard convention, according to the control and event procedure name it's given. IProcName = Me.Controls(mpControl.Name).EventProcPrefix & "_" & ipEventProcNameĬase "Click", "AfterUpdate", "Change", "GotFocus", "LostFocus", "Enter":ĬallByName Me, iProcName, VbMethod, mLastKeyPressedAsciiĭebug.Print "Multi event handling cannot support the " & ipEventProcName & " event."ĭebug.Print "Procedure " & Forms("Form1").Controls(mpControl.Name).EventProcPrefix & "_" & ipEventProcName & _ Private Function FireControlSingleEvent(ByRef mpControl As Control, _ That FireControlSingleEvent is the important part (the name is a bit awkward, suggestions welcome!) - this fires the control's 'unique' event handler, so I can add handling for a specific control. MpTextBox.BorderColor = RGB(100, 150, 215)įireControlSingleEvent mpTextBox, "GotFocus" Next, let's add the 'master' event handler: Public Function AnyTextBox_GotFocus(ByRef mpTextBox As TextBox) Don't worry about the details of my Hungarian notation either.

assign keyup to multiple textboxes

Not relevant to the question, but BuildControlCollection here turns iTextBox into a collection of all the text boxes on the form. LTextBox.OnGotFocus = "=AnyTextBox_GotFocus(" & lTextBox.Name & ")" Private iTextBoxes As CollectionīuildControlCollection Me, iTextBoxes, eTextBox We can then loop through it and assign all of their event handlers to a single handler programatically. It isn't perfect, but the caveats of this are much smaller than the caveats of the above.įirst off, we want to build a collection of the controls we want to use. Shudder.Īfter a long time rolling it around in my head, I think I have a solution. Unless I whack a massive Switch statement inside the loop or something. I also lose the ability to grant specific behaviour to an individual control. I'd need to loop through the form's entire control collection every single time this thing fires. ' ? Which text box just fired this? I have no way of knowing!

assign keyup to multiple textboxes assign keyup to multiple textboxes

The other option is to set the handler for every text box to a constant expression in the form designer (eg =AnyTextBox_HandleGotFocus()), but doing so makes you lose which text box actually got the focus: Private Sub AnyTextBox_HandleGotFocus() and as a programmer I'm too lazy to do it like this. And it's time-consuming, boring, easy to forget to do. That creates a lot of duplicate code, fast. The brute-force option would be to manually add a handler to every text box, and have them all call a master handler ( ).

Assign keyup to multiple textboxes code#

The code for changing one text box border on focus is easy: Private Sub TextBox0_GotFocus()īut doing this for every single text box causes a problem. For sake of example, let's say we want every text box to change its border color when it gains focus. The two 'easy' methods have glaring issues. How could you have one single event handler to handle every control (or all of a particular type of control) on a form? I've never been able to find a complete solution online either. Bit of background info first - I've been struggling with a problem for a while now.










Assign keyup to multiple textboxes