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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Public Function To Change Forecolor on command button

Status
Not open for further replies.

Sullaway

Technical User
Sep 27, 2000
50
0
0
US
Wondering if someone could lend me a hand in coding a public function that when a command button gets focus it changes the forecolor and when it loses focus it goes back to it's original color. I have read help and searched on several forums and found code similar but I'm too green to change what I'm finding and get it to work for me.
 
Use the GotFocus and LostFocus events for the command button.

On GotFocus, run Command1.ForeColor = 255 and on LostFocus, use Command1.ForeColor = 0.

This assumes the command button's name is Command1. The text will appear in red when the button has the focus.

If you need any more details, let me know.

HTH
John

Use what you have,
Learn what you can,
Create what you need.
 
Another method is to use a Function, a Form Module level variable and the MouseMove Event of each Control on the Form.

Option Compare Database
Option Explicit

Dim mstPrevControl As String

Function fSetColor(stControlName As String)

On Error Resume Next
With Me(mstPrevControl)
.ForeColor = 0
End With
mstPrevControl = stControlName
With Me(stControlName)
.ForeColor = 255
End With
End Function

Function fRemoveColor()
On Error Resume Next
With Me(mstPrevControl)
.ForeColor = 0
End With
End Function


Then in the MouseMove Event of the Controls you want to change, you call the Function fSetColor and pass the name of the Control (i.e. =fSetColor("cmdExit")

You must also call the Function fRemoveColor() on the other controls that the mouse might pass over (i.e. Detail Section), in order to restore the color back to normal for the Control you had changed. You do not need to pass the name of the Control when using this Function, because it uses the name of the Control in the variable mstPrevControl.

This code is set up to be used in a Form, not a Module, due to the use of the "Me" shortcut.

PaulF
 
Thanks Paul, I believe this is what I was looking for.

Shane
 
Shane,

Be sure to set the command buttons' tabstop properties to 'no' so they don't get the focus without the mouse event.

John

Use what you have,
Learn what you can,
Create what you need.
 
Or duplicate the code for the button Got Focus and Lost Focus events- this leaves both options open.....
 
Shane,

I was thinking about this last night. When I first responded, I hadn't tracked on the Public Function aspect of the question. I assume that is to avoid having to write the code on each command button's event.

The following could be placed in the Form's Timer event module and with the Form's TimerInterval set to about 250, it will continually check to see if the active control is a command button and set its forecolor to red if it is.

Code:
Dim ctl as Control

If Me.ActiveControl.ControlType = acCommandButton Then
  For Each ctl in Me.Controls
    If ctl.Name <> Me.ActiveControl.Name Then
       ctl.ForeColor = 0
    Else
       ctl.ForeColor = 255
    End If
  Next ctl
End If

If the TimerInterval setting is too low, the form tends to blink. If it's too high, there is a lag when you tab from one button to the next. 250 worked for me, but play with it.

If you only want some of the buttons to have this color-changing property, you can use the Tag property. Set the Tag property for those buttons to something like &quot;Change&quot; and instead of checking the Control's type on the first If...Then statement, check its Tag property.
EG:
Code:
  If Me.ActiveControl.Tag = &quot;Change&quot; Then 
     For Each ctl in Me.Controls...

HTH
John

Use what you have,
Learn what you can,
Create what you need.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top