ForeColor change on DoubleClick in Combobox

stevekos07

Registered User.
Local time
Today, 08:21
Joined
Jul 26, 2015
Messages
174
Hello. I have been having trouble accomplishing what I thought would be a fairly simple task. I have a form with a combobox, which I want to be able to change the forecolor of on double-click. So if it is black, doubleclick would turn the font red and vice versa. This is the code I have for it so far but it doesn't seem to be working. Any assistance would be greatly appreciated.

Private Sub Combo0_DblClick(Cancel As Integer)
Select Case ForeColor
Case "#ED1C24"
Me.Combo0.ForeColor = "#000000"
Case "#000000"
Me.Combo0.ForeColor = "#ED1C24"
End Select
End Sub
 
Hi. Maybe you meant to say?
Code:
Select Case Me.Combo0.ForeColor
 
I tried the RGB designations instead of Hexadecimal, plus I added the Me.Combo0 as you indicated and that fixed it. Thanks.
 
I tried the RGB designations instead of Hexadecimal, plus I added the Me.Combo0 as you indicated and that fixed it. Thanks.
Congratulations! Glad to hear you got it sorted out. Good luck with your project.
 
I hate to ask the question, but are you expecting the background color to "stick"?
 
I hate to ask the question, but are you expecting the background color to "stick"?
Thanks for the comment. That's not necessary, but I might look at that later. There are quite a few fields that this is being applied to so the programming for that might be a bit too much for now.
 
There are quite a few fields that this is being applied to so the programming for that might be a bit too much for now.
You should only have to write a single function and not multiple event procedures to apply this to more than one control. In other words one control or a hundred controls should be the same amount of code needed.
 
Ok. So if I wanted the colour to "stick" on saving and re-opening, how should I write the code?
 
AFAIK, you would have to save the property value somewhere if this is based on an arbitrary double click because unless you set it in design view, it won't stick. So on form open, you'd have to get whatever value you've stored to affect the forecolor. That could be a table field or you could store it in the control Tag property. If this is really based on data, then conditional formatting might work, but you didn't say what kind of form it is.
 
Regarding the colours, if you are using the standard ones in vba, they could be enters as vbRed for Red and vbNormal for Black

Code:
Me.Combo0.ForeColor = vbRed
 
Making the colors "stick" would require modifying your table to contain a color value for every field you want to colorize. Then in the current event of each record, you would loop through the table fields and apply the color. My advice, don't go there.
 
There is always the tag property, as I mentioned. Not that I would do that either.
 
When faced with a similar (but not identical) problem, I identified a "form state" that would provide guidelines for ALL controls. In my statuses, a form could be "Normal" (not dirty and not new data), "Virgin" (new data and dirty, based on new record), "Dirty" (not new data but something has changed), "Locked" (user is not allowed to diddle with the form), and "Incomplete" (at least one required field not filled yet). That form status allowed me to decide the color schemes for each control that had a .Value (explicit or implied). Then I could use a "For Each ctl In Me.Controls" construct and reset colors for everything that COULD have a color change under some circumstance.

The colors "stuck" because they got reasserted during the Form_Current routine based on the state of the form at that moment. As it happens, the "Current" event fired for my form's "Normal" and "Locked" and "Virgin" states. Normal and Locked depended on my user security setup. "Virgin" also went through Current because you got there by doing a DoCmd to GoTo a new record - and when it did, the form was Virgin by my definition.
 
+1. I also have a SetFormState that runs on load, on status change, and a couple of other events. It mostly sets various controls' enabled property but can be used for anything.
 
See if this is of interest. Shows how to pick different settings and same them so you can change the control format. The font picker and color picker may be of interest. I doubt I would ever do this, but some people may like this. Would allow you to configure the display for different conditions. Probably can do some better using themes.
 

Attachments

Last edited:

Users who are viewing this thread

Back
Top Bottom