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

Change Colour on Focus Function

Status
Not open for further replies.

faztech

Programmer
May 21, 2001
22
AU
Does anyone know of a function that will detect the change of focus of a control on a form and then set the backcolour of the control. I know about lost and got focus but this is for a large application with about 30 forms and that means for ever control i must write two function. Anyone know a better way to do it. This is what I am trying to do

I am trying to write a function for forms that contain textboxes, combo boxes and checkboxes. I want to make the backcolour of the control that has focus blue and white for the other controls. Or basically want to set it to blue when it has focus and white when it loses focus without having to do the lost and got focus for each control.

Is there a generic function that I could use

Thanks Always

Faztech FAZTECH
:p
 
Not that I know of. If you're trying to save yourself some code, then you may want to go to the extreme of making your own custom ActiveX controls that perform this functionality. Otherwise, the focus events must be called. Neil Konitzer, President
Freisoft
 
Actually, you do need ONE function, however it can be a generic function called from each controls "GotFocus" event.

Send the function the form name and the control name.

In the function, itterate through the controls collection for the form, checking wheather the control has a BackColor property and setting ALL of them to the default (White). Now check the control name to see if it has the BackColor property, and set it to the "HighLight" color (Blue) if the property exists.

This WILL slow up your program, but only enough to set the controls on the specified form.

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Thanks Michael Reid, over the last couple of days I have been looking it up and you are right on the bullet. I have written a function (below) that works quite well. I just placed it in a module and call it on all textbox and combobox got focus events. Thanks for your help

///Placed this in the module

Public Function changeColour(ctlFocus As Control, frm As Form)
Dim ctl As Control
Dim i As Integer

For Each ctl In frm.Controls
If TypeOf ctl Is TextBox Or TypeOf ctl Is ListBox Or TypeOf ctl Is ComboBox Then
If ctlFocus = ctl Then
ctlFocus.BackColor = vbBlue
Else
ctl.BackColor = vbWhite
End If
End If
Next ctl

End Function


///// And on all got focus events call this function
Private Sub Text1_GotFocus()
changeColour Me.ActiveControl, Me
End Sub

Thanks Again

Simon
FAZTECH
:p
 
Perhaps?
Code:
Public Sub changeColour(ctlFocus As Control)
    Static ctlHadFocus as control
    If Not (ctlHadFocus is Nothing) then       
        ctlHadFocus.Backcolor = vbWhite
    End if

    If TypeOf ctlFocus Is TextBox Or _
       TypeOf ctlFocus Is ListBox Or _
       TypeOf ctlFocus Is ComboBox Then
        ctlFocus.BackColor = vbBlue
        set ctlHadFocus = ctlFocus
    End If
End Sub

///// And on all got focus events call this function
Private Sub Text1_GotFocus()
    changeColour ActiveControl
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top