Foreground-Background Color Values in a module

  • Thread starter Thread starter RLN
  • Start date Start date
R

RLN

Given the following code:
Private Sub REGION_LostFocus()
'change the label to this foreground/background
'color when the control has focus.
lblRegion.BackColor = 12632256
lblRegion.ForeColor = 8388608
End Sub

Private Sub REGION_GotFocus()
lblRegion.BackColor = 32768
lblRegion.ForeColor = 16777215
End Sub

The above code is for one control.
I'd like to use these for other controls on my form.
Is there a simple way to call these in a module so to eliminate any possible
numeric errors?

Thanks.
 
Declare four constants at the top of your form module:

Const cLostFocusBackColor = 12632256
Const cLostFocusForeColor = 8388608
Const cGotFocusBackColor = 32768
Const cGotFocusForeColor = 16777215

You can then use these constant names instead of the numbers in your code.

If the labels are "attached" to the controls that are getting/losing the
focus then you could be a bit tricksier:

Private Function SetLabelColors ( fGotFocus as Boolean )
Dim lbl as Label
On Error Resume Next
Set lbl = Me.ActiveControl.Controls(0)
If fGotFocus Then
lbl.BackColor = cGotFocusBackColor
lbl.ForeColor = cGotFocusForeColor
Else
lbl.BackColor = cLostFocusBackColor
lbl.ForeColor = cLostFocusForeColor
End If
End Function

Now, for every control that has a label attached, set the GotFocus property
to:
=SetLabelColors(True)
and set the LostFocus property to:
=SetLabelColors(False)
 
Graham,

That worked very well. Thank you.

The labels whose foreground/background I wish to modify are actually
separate from the text box controls.
 
Thanks for the feedback.

Even if the labels are not attached, you can do it with a single procedure
if you wish:

Private Function SetLabelColors ( sLabelName as String, fGotFocus as
Boolean )
Dim lbl as Label
On Error Resume Next
Set lbl = Me.Controls(sLabelName)
If fGotFocus Then
lbl.BackColor = cGotFocusBackColor
lbl.ForeColor = cGotFocusForeColor
Else
lbl.BackColor = cLostFocusBackColor
lbl.ForeColor = cLostFocusForeColor
End If
End Function

Then, for the textbox REGION:

GotFocus: =SetLabelColors( "lblRegion", True)

LostFocus: =SetLabelColors( "lblRegion", False)
 
Back
Top