Automatically highlight control with focus

timh

Well-known member
Joined
Nov 28, 2008
Messages
50
Programming Experience
Beginner
Is there a way to highlight the active control (e.g. changing the BackColor property of a text box, to aid user input) without having to manually do it in each control's GotFocus and LostFocus event?

Hopefully the answer is "Yes", even if that means I'll be embarrassed at asking an obvious question...
 
You could redirect all the GotFocus and LostFocus events to a single Sub and use the sender variable (that is what it's there for after all).
 
You could redirect all the GotFocus and LostFocus events to a single Sub and use the sender variable (that is what it's there for after all).

Am I missing something, or are you saying that I need to do it like this...

VB.NET:
Private Sub TextBox1_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox1.GotFocus
   call subfocus
End Sub
.
.
.
Private Sub TextBox10_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox10.GotFocus
   call subfocus
End Sub

Private Sub TextBox1_LostFocus(sender As Object, e As System.EventArgs) Handles TextBox1.LostFocus
   call sublost
End Sub
.
.
.
Private Sub TextBox10_LostFocus(sender As Object, e As System.EventArgs) Handles TextBox10.LostFocus
   call sublost
End Sub

I was hoping that for all the text boxes within my custom control, I would be able to trap a generic GotFocus/LostFocus event and do the necessary. If that's what you're saying I can do, then please could you help me out by explaining a bit more?

If I sound very ignorant, that's because I am; I pick up VB as I go along, with a handy reference book alongside me!

I will stop writing at this point, as it's late and I'm getting in a knot just trying to explain myself!
 
No, you do it like this


Private Sub TextBox1_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus, Textbox3.GotFocus etc. etc.
sender.BackColor = Color.BlanchedAlmond
End Sub
 
Dunfiddlin is quite correct that you can add multiple events to the Handles clause of a single method. They don't necessarily even have to be the same event, as long as the signatures are compatible. You can add those events manually if you want, but that's likely to be a bit tedious if you have a large number of controls, so you might want to use the designer instead. In that case, select all the relevant controls in the designer using Shift+Click+Drag or Ctrl+Click, open the Properties window, click the Events button and then double-click the appropriate event. You can also use the drop-down list for an event to select an existing event handler for one or more selected controls.

That might still be a bit tedious if you have a very large number of controls, in which case you might want to leave attaching the event handlers until run time. In that case you might like to check this out:

Visit Every Control on a Form (includes nested controls, no recursion)

You can use that to visit every control and use AddHandler each time to add the event handler. Don't forget to do the same when closing the form and use RemoveHandler to remove the event handler.

Also, as the documentation clearly states, you almost certainly should not be handling the GotFocus and LostFocus events, but rather the Enter and Leave events.
 
No, you do it like this


Private Sub TextBox1_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus, Textbox3.GotFocus etc. etc.
sender.BackColor = Color.BlanchedAlmond
End Sub

Ah-ha! That's what I call a lightbulb moment. Now I understand; that's something I could have used time and time again. I guess that's the difference between pros and amateurs. Or pros and me at any rate!

Thanks very much for explaining that.

Dunfiddlin is quite correct that you can add multiple events to the Handles clause of a single method. They don't necessarily even have to be the same event, as long as the signatures are compatible. You can add those events manually if you want, but that's likely to be a bit tedious if you have a large number of controls, so you might want to use the designer instead. In that case, select all the relevant controls in the designer using Shift+Click+Drag or Ctrl+Click, open the Properties window, click the Events button and then double-click the appropriate event. You can also use the drop-down list for an event to select an existing event handler for one or more selected controls.

That might still be a bit tedious if you have a very large number of controls, in which case you might want to leave attaching the event handlers until run time. In that case you might like to check this out:

Visit Every Control on a Form (includes nested controls, no recursion)

You can use that to visit every control and use AddHandler each time to add the event handler. Don't forget to do the same when closing the form and use RemoveHandler to remove the event handler.

Also, as the documentation clearly states, you almost certainly should not be handling the GotFocus and LostFocus events, but rather the Enter and Leave events.

Thanks for that too. I will need to revisit my code and look for instances where I have used these events. I have already replaced some "LostFocus" events, because I got funny results.

I do appreciate all the help and advice.
 
Back
Top