Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

general solution for changing the backcolor of the current control

Status
Not open for further replies.

flbos

Programmer
Sep 15, 2005
41
NL
Hello,

I was wondering if there is a general solution for changing the backcolor of the current control (in which the cursor is) and changing it back to normal when the focus is lost?

I could define a gotfocus and lostfocus event for each control but this is quite a lot of work when I need to do this for hundreds of controls that are in my application.

Is there a way to define a procedure somewhere that applies to all the controls?
 
It is possible to have multiple event handlers for one event on one control. For example:

Private Sub Click1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'do some stuff

End Sub

Private Sub Click2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'do some other stuff

End Sub

Private Sub Click3(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'do some more stuff

End Sub

When you click Button 1, all three of these event handlers will execute. I am not sure of the order of execution, or if there is a way to control which executes first.

You can also do this with the AddHandler function, so you don't have to put hundreds of controls in the "Handles" part of the sub/function definition.

Okay, after a little experimentation it seems that if you use the AddHandler method, the events fire in the order in which the event handlers were added. For example:

AddHandler Button1.Click, AddressOf Click3
AddHandler Button1.Click, AddressOf Click1
AddHandler Button1.Click, AddressOf Click2

will fire Click3, then Click1, then Click2 when Button1 is clicked.

Hope this helps.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
You could also create a sub and just add handles to whichever controls you want it to handle e.g.
Code:
    Private Sub GlobalGotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus, Button1.GotFocus
        CType(sender, Control).BackColor = Color.Blue
    End Sub


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top