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!

Setting Backcolor property 2

Status
Not open for further replies.

sko

Technical User
Dec 9, 2000
283
0
0
US
I want to highlight the control that has the focus on my forms by changing the backcolor. I'm using the GotFocus, LostFocus events for each control. This works fine but can be tedious. Is there some way to shorten the code by using the controls collection or maybe the ActiveControl property?
 
This may not help much, but you could add the two functions below, the just call the function with the On Got Focus and On Lost focus events:

Public Function Highlight()
Me.ActiveControl.BackColor = 65535 'Yellow
End Function

Public Function UnHighlight()
Me.ActiveControl.BackColor = 16777215 ' White
End Function

In the control's On Got Focus event property: =Highlight()
In the control's On Lost Focus event property: =UnHighlight()
You can change the colors in the above code to whatever you want.

This won't save much work but it will be some.

HTH X-)
RDH
Ricky Hicks
rdhicks@mindspring.com

 
A quick and easy way is to select all of the fields you want to have a focus color applied to and then set the back color to the color you want to be the focus color. Then before you do anything else change them all back to transparent. Run the form and watch what happens when you tab around from control to control.

cool!!
John A. Gilman
gms@uslink.net
 
Pretty darn cool.....John. Thanks for the tip.

RDH Ricky Hicks
ricky@athree.com

 
John,

Verry cool !

Im just curious... did you stumble on this by accident ?


Dave
gallagherd@earthlink.net
 
Yes and no. I ran across the behavior first and then found a printed article on it somewhere. It seems Access will remember the backcolor value of the last selection you made.
It looks weird if you choose several different backcolors for several text boxes. That is how I found it. Although this only works if the final setting is transparent.
John A. Gilman
gms@uslink.net
 
This trick actually takes advantage of a little known fact, and what may be a bug in Access. The fact is that Access uses a different forms engine from Windows. Controls on standard Windows forms are always Windows windows, that is, each control has a Window data structure, and Windows manages all the built-in things like scrolling and resizing the frame. This takes up a lot of the Windows GDI resources. Access uses its own forms engine, which simulates a Windows window. This saves a lot of resources. But in order to avoid having to duplicate the logic that handles all the built-in functions, Access creates a real Windows window when you give the control the focus. In other words, focused controls are real Windows windows, while unfocused controls are static bitmap images created by Access.

So the reason this cool trick works is that, when the control is unfocused, Access repaints it, honoring the Transparent BackStyle property and ignoring the BackColor. But when it's focused, Windows repaints it, and (for a text box, at least) Windows ignores BackStyle and honors BackColor. I don't know if this was by design or by accident; the Help file doesn't describe this behavior at all. If it was accidental, it's a bug, and may be corrected in a future version. But it's such a cool feature that it's worth using, even if it stops working this way some day. The worst that can happen is that you'll have to go back in and write all the laborious GetFocus and LostFocus code if you port your database to a later version. Rick Sprague
 
Rick Sprague, wow!

Just another note. I wonder how many Access 2000 users are familar with the conditional formatting available that can give a wide range of formating capabilities to data sheet or Continuous forms?
John A. Gilman
gms@uslink.net
 
Rick Sprague, send me an email if you would be interested in kicking around some questions regarding ODBC connection problems using Access and SQL Server.
John A. Gilman
gms@uslink.net
 
This is really great! Questions: I want the controls to be a different color than the background (background,stone; controls, gray) but I would love to have the highlights, too, but can't seem to get it to work that way. I also tried the above code, but I must be missing some OnGotFocus code as when I put in the above it doesn't work. I'm really a beginnner with this code stuff :). Would someone write it for me? Thanks!
Jackie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top