change backcolor of controls which have focus

S

Stefan

hello,
Just a sort of newbie question.
How can i change the backcolor of the controls which have the focus
(textbox/combobox/datetimepicker) and
set it back to white when they lost focus.
thanx in advance
Stefan
 
C

Chris, Master of All Things Insignificant

Use these events:

Private Sub Button1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.GotFocus
Button1.BackColor = .....
End Sub
Private Sub Button1_LostFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.LostFocus
Button1.BackColor = .....
End Sub


Hope it helps
Chris
 
J

Jay B. Harlow [MVP - Outlook]

Stefan,
As Chris suggests you can use the GotFocus & LostFocus events for this.

To minimize duplicate code I would consider using:
1. a single event handler for all controls
2. derived controls
3. an ExtenderProvider (implement System.Component.IExtenderProvider)

With #1 you need to add the same "handler" on each form, plus wire all the
controls to the handler.

With #2 you need to derive from every control & remember to use these custom
controls.

With #3 its more work to create the Provider itself (then a single derived
control), however it is then really easy to use the provider on multiple
forms.

I normally favor #2 as invariable I am adding other customizations to the
controls...

Both #2 & #3 are "designer friendly"!

Hope this helps
Jay
 
C

Cor Ligthert

Stefan,

When you use this class in your application
\\\
Public Class MyBackGroundColors
Public Sub doSet(ByVal parentCtr As Control)
Dim ctr As Control
For Each ctr In parentCtr.Controls
AddHandler ctr.LostFocus, AddressOf meLostFocus
AddHandler ctr.GotFocus, AddressOf meGotFocus
doSet(ctr)
Next
End Sub
Private Sub meLostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
DirectCast(sender, Control).BackColor = SystemColors.Window
End Sub
Private Sub meGotFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
DirectCast(sender, Control).BackColor = Color.LightBlue
End Sub
End Class
////
And than this row in every load event of your startupforms.
\\\
Dim mycol As New MyBackGroundColors
mycol.doSet(Me)
Dim frm1 As New Form2
mycol.doSet(frm1) 'and than this for every form you create.
frm1.Show()
////
Than I think you have done it for every form and all your controls.

I hope this helps?

Cor
 
S

Stefan

how can i prevent with your solutions buttons
get coloured to. now i'm using the tag property
For Each ctr In parentCtr.Controls
 
C

Cor Ligthert

Stefan,

It is easier to use
\\\
If Not TypeOf ctr Is Button then .........
///

I hope this helps?

Cor
 
S

Stefan

I use Cor's solution.It works fine
thanks to everyone for responding so quick.
greetz,
Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top