Change font color on ALL form controls on "Got Focus" and "Lost Focus" (1 Viewer)

KP_SoCal

Registered User.
Local time
Today, 15:35
Joined
Dec 4, 2009
Messages
39
Change font color on ALL form controls on "Got Focus" and "Lost Focus"

On my form I have numberous command button controls. As you can see from my code below, I'm applying one style of font and color when the control has focus and I'm applying another style when it loses focus.

To accomplish this, I'm re-writing this for each control. I know I could write another routine for focus and lost focus and call it for each control, but would it be possible to write one block of code that would apply a particular font style to ALL command button controls in my form?

Thanks!

Code:
Private Sub cmdEmail_GotFocus()
  With cmdTransferCheck
    .ForeColor = RGB(0, 0, 255)
    .FontBold = True
  End With
End Sub

Private Sub cmdEmail_LostFocus()
  With cmdTransferCheck
    .ForeColor = vbBlack
    .FontBold = False
  End With
End Sub
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 23:35
Joined
Sep 12, 2006
Messages
15,634
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

dim ctrl as control

for each ctrl in me.controls
'do whatever you want with the controls
next
 

ajetrumpet

Banned
Local time
Today, 17:35
Joined
Jun 22, 2007
Messages
5,638
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

you don't need code at all. highlight all of the controls at one time, right click on the form, and do some conditional formatting.

there is a option in there for formatting when the control has the focus. with lost focus events though, this will not work and you'll need a code loop like you see below.
 

KP_SoCal

Registered User.
Local time
Today, 15:35
Joined
Dec 4, 2009
Messages
39
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

Dave, I understand how to apply this code say if I click a command button to fire the code, it would adjust the font color as needed (see example code below). But I'm not sure how to apply this with the "focus" and "lost focus" events. I know I could individually code out a sub routine for each control focus and lost focus events, but if there's away to apply this in one block of code, that would be great.

Code:
Private Sub cmdButton_Click()
Dim ctl As Control
For Each Ctl in Controls
ctl.ForeColor = RGB(255, 255, 0)
Next ctl
End Sub
So this code changes all my controls to blue font, but I have to click cmdButton to accomplish this. I want to accomplish this strictly but focus and lost focus.

******************************************************
Adam, I don't think conditional formatting will be an option for me for command buttons. Text, List, or Combo box controls works fine, but not on command buttons. Here's a nice article off of Microsoft's website on how to set conditional formatting.
 

ChrisO

Registered User.
Local time
Tomorrow, 08:35
Joined
Apr 30, 2003
Messages
3,202
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

Something to try.

Behind each Form: -

Code:
Option Explicit
Option Compare Text


Private Sub Form_Open(ByRef intCancel As Integer)

    InitialiseEvents Me

End Sub


In a Standard Module: -

Code:
Option Explicit
Option Compare Text


Public Sub InitialiseEvents(ByRef frmThisForm As Form)
    Dim ctl            As Control
    Dim strControlName As String
    
    For Each ctl In frmThisForm
        On Error Resume Next
        ctl.OnGotFocus = "=HandleFocus('" & frmThisForm.Name & "', '" & ctl.Name & "', 'Got')"
        ctl.OnLostFocus = "=HandleFocus('" & frmThisForm.Name & "', '" & ctl.Name & "', 'Lost')"
        Err.Clear
    Next ctl

End Sub


Public Function HandleFocus(ByVal strFormName As String, _
                            ByVal strControlName As String, _
                            ByVal strChange As String)

    With Forms(strFormName)(strControlName)
        Select Case strChange
            Case "Got"
                .ForeColor = vbBlue
                .FontBold = True
            
            Case "Lost"
                .ForeColor = vbBlack
                .FontBold = False
        End Select
    End With

End Function

Regards,
Chris.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 23:35
Joined
Sep 12, 2006
Messages
15,634
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

you can do this on a button click, or in an open event, say

but if you want to change the setting as you enter/leave each control, then you have to put the functionality on each control, so it can be a lot of work.

i think!
 

ChrisO

Registered User.
Local time
Tomorrow, 08:35
Joined
Apr 30, 2003
Messages
3,202
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

Dave.

If it’s done on the Form Open event of each Form the above code will soft code all GotFocus and LostFocus events for that form.

If you open a Form in Form view, with the property sheet open, you can see what it has done to each event property when you select each control.

Worth a try…
 

KP_SoCal

Registered User.
Local time
Today, 15:35
Joined
Dec 4, 2009
Messages
39
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

Ok guys, I'll give this a shot. Thanks for your help! :)

KP
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 23:35
Joined
Sep 12, 2006
Messages
15,634
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

Dave.

If it’s done on the Form Open event of each Form the above code will soft code all GotFocus and LostFocus events for that form.

If you open a Form in Form view, with the property sheet open, you can see what it has done to each event property when you select each control.

Worth a try…

I was thinking more of how to manage a situation where say - you wanted to change the border and background of the active control, and change this as you moved from control to control - you would need some code attached either to the on enter/exit events or the got focus/lost focus events - but you would need to add it to each control.
 

ChrisO

Registered User.
Local time
Tomorrow, 08:35
Joined
Apr 30, 2003
Messages
3,202
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

Dave.

You should be able to just add to the list in Function HandleFocus: -

Code:
Option Explicit
Option Compare Text


Public Sub InitialiseEvents(ByRef frmThisForm As Form)
    Dim ctl As Control
    
    On Error Resume Next

    For Each ctl In frmThisForm
        ctl.OnGotFocus = "=HandleFocus('" & frmThisForm.Name & "', '" & ctl.Name & "', 'Got')"
        ctl.OnLostFocus = "=HandleFocus('" & frmThisForm.Name & "', '" & ctl.Name & "', 'Lost')"
    Next ctl
    
    Err.Clear

End Sub


Public Function HandleFocus(ByVal strFormName As String, _
                            ByVal strControlName As String, _
                            ByVal strChange As String)

    Static lngForeColour   As Long
    Static lngFontWeight   As Long
    Static lngBorderStyle  As Long
    Static lngBorderColour As Long
    Static lngBackStyle    As Long
    Static lngBackColour   As Long
    
    On Error Resume Next

    With Forms(strFormName)(strControlName)
        Select Case strChange
            Case "Got"
                [color=green]' Save current configuration.[/color]
                lngForeColour = .ForeColor
                lngFontWeight = .FontWeight
                lngBorderStyle = .BorderStyle
                lngBorderColour = .BorderColor
                lngBackStyle = .BackStyle
                lngBackColour = .BackColor
                
                [color=green]' Set required configuration.[/color]
                .ForeColor = vbBlue
                .FontWeight = 700
                .BorderStyle = 1
                .BorderColor = vbRed
                .BackStyle = 1
                .BackColor = vbYellow
            
            Case "Lost"
                [color=green]' Restore saved configuration.[/color]
                .ForeColor = lngForeColour
                .FontWeight = lngFontWeight
                .BorderStyle = lngBorderStyle
                .BorderColor = lngBorderColour
                .BackStyle = lngBackStyle
                .BackColor = lngBackColour
                
        End Select
    End With
    
    Err.Clear

End Function

Small A2K3 demo attached.

Regards,
Chris.
 

Attachments

  • CommonControlTest.zip
    60.2 KB · Views: 956

KP_SoCal

Registered User.
Local time
Today, 15:35
Joined
Dec 4, 2009
Messages
39
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

Chris, this is outstanding! Thanks for providing the example db. This is exactly what I needed to accomplish!!!

:D

KP
 

ChrisO

Registered User.
Local time
Tomorrow, 08:35
Joined
Apr 30, 2003
Messages
3,202
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

Glad it worked okay but try this link to see how it works.

Regards,
Chris.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 23:35
Joined
Sep 12, 2006
Messages
15,634
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

chris - thats a neat way of handling it - good stuff

the way i'd seen it done before was to manually set the event properties for the controls - so I never bothered.
 

NigelShaw

Registered User.
Local time
Today, 23:35
Joined
Jan 11, 2008
Messages
1,573
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

Hi

I would write the code to change the colours as single routines
Code:
Sub ColourOn()
'my code to change colour
'and other stuff here
End Sub

Sub ColourOff()
'code to change it all back
End Sub

my OnOpen event would call ColourOff to ensure the right formatwhen opened and the

GotFocus would call

ColourOn

LostFocus would call

ColourOff

this means that if you need to add or change the format for focussing, you only need to do it once and anything that calls it will Be correct. It's also more organized as you don't have many sets of the same code and you have less code.

I have a routine I'll post in a while.

Regs

Nidge
 

ChrisO

Registered User.
Local time
Tomorrow, 08:35
Joined
Apr 30, 2003
Messages
3,202
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

Eagerly awaiting your routine…
 

NigelShaw

Registered User.
Local time
Today, 23:35
Joined
Jan 11, 2008
Messages
1,573
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

Hi

sorry. Busy time of year! I'll post it later when I get home. It will be after 7.00pm though.

Regs

Nidge
 

NigelShaw

Registered User.
Local time
Today, 23:35
Joined
Jan 11, 2008
Messages
1,573
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

Hi

routine -

Code:
Private Sub Control_Focus()
    Dim lngGreen As Long
    Dim lngOrange As Long
    
    lngBorderColor = Me.ActiveControl.BorderColor

    lngGreen = RGB(154, 205, 50)
    lngOrange = RGB(255, 211, 155)
    Me.ActiveControl.Value = ""
    Me.ActiveControl.BorderStyle = 1
    Me.ActiveControl.BorderWidth = 2
    Me.ActiveControl.BorderColor = lngOrange
    'Me.ActiveControl.FontItalic = True
    Me.ActiveControl.FontWeight = 700
    Me.ActiveControl.FontSize = 10
    Me.ActiveControl.ForeColor = black
    Me.ActiveControl.BackColor = RGB(255, 255, 204)
    '    Dim lngGreen As Long
    '    lngGreen = RGB(181, 203, 136)
    '    Me.ActiveControl.Properties("BackColor") = lngGreen
End Sub

    '##Control Lost Focus##
Private Sub Control_Not_Focus()

    '    Me.ActiveControl.Properties("BackColor") = lngWhite
    Dim lngWhite As Long
    Dim lngGrey As Long

    lngWhite = RGB(255, 255, 255)
    lngGrey = RGB(205, 200, 177)

    Me.ActiveControl.BorderStyle = 1
    Me.ActiveControl.BorderWidth = hairline
    Me.ActiveControl.BorderColor = lngBorderColor
    'Me.ActiveControl.FontItalic = False
    Me.ActiveControl.FontWeight = 400
    Me.ActiveControl.FontSize = 9
    Me.ActiveControl.ForeColor = black
    Me.ActiveControl.BackColor = lngWhite
End Sub
call
Code:
Control_Focus
to change font and styles

call
Code:
Control_Not_Focus

HTH


Nigel
 

ChrisO

Registered User.
Local time
Tomorrow, 08:35
Joined
Apr 30, 2003
Messages
3,202
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

Thanks, Nigel.

Just so I understand your code better…

If I have a text box called Text2 I would need the following two event handlers: -

Code:
Private Sub Text2_GotFocus()
    Control_Focus
End Sub

Private Sub Text2_LostFocus()
    Control_Not_Focus
End Sub

And if I have a text box called Text3 I would also need the following two event handlers: -

Code:
Private Sub Text3_GotFocus()
    Control_Focus
End Sub

Private Sub Text3_LostFocus()
    Control_Not_Focus
End Sub

Is that the way you would use it?

Regards,
Chris.
 

ChrisO

Registered User.
Local time
Tomorrow, 08:35
Joined
Apr 30, 2003
Messages
3,202
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

Well that’s what I thought; there are 4 text boxes and 8 event handlers.

So if we go with that method and if we had 600 text boxes we would need 1200 event handlers. And if we wanted Mouse Move events that would be another 600 event handlers meaning we would need 1800 event handlers in total.

I don’t think that would be possible but even if it is I would not do it that way.

Attached is another demo, similar to the one in post #10, which has the 600 text boxes with Got Focus, Lost Focus and Mouse Move. Look at the code behind the Form, there are no individual event handlers.
 

Attachments

  • CommonControlTest.zip
    49.6 KB · Views: 448

Users who are viewing this thread

Top Bottom