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

Setting the background colour of the cells in one column of a grid. 1

Status
Not open for further replies.

AndrewMozley

Programmer
Oct 15, 2005
621
GB
I have a grid where the user can navigate down a column with a view to entering a value in the current cell. Some rows are description rows, so a value may not be entered, while others specify a product so a value can be entered.

I have set the ReadOnly property of the column in the AfterRowColumnChange() of the grid and this allows / disallows data entry (Thank you EzLogic), and I would also like now to colour the cells so that the ineligible cells are greyed out.

Setting the DynamicBackColor property of the Column shows which cells are eligible. However, if the user navigates up and down (with Uparrow, Dnarrow), the cell with focus always gets the backcolor of light blue (so it is no longer getting its colour from DynamicBackColor); I would like this navigation to continue to colour the ineligible cells a light gray.

Hope I have explained this properly! Thanks
 
Try to change the Sparse property for the column. Unfortunately I am never able to remember, it's either .T. or .F.
 
Well, you can't combine DyanmicBackcolor and HightlightColor. At the active row it's always the latter, so you'd have to change the HighlightColor in AfterRowColchange to a darker blue or the grey or turn off the Highlight coloring overall, DynamicBackColor will still apply then, it's older than the Highlight... properties and will even have effect with Themes turned on, but it is overriden in the active row with the newer HightLight-Settings.

Bye, Olaf.
 
Each Column in the Grid has a DynamicBackColor property that can be used with an Expression to change the row-cell background color (RGB) based on the Expression being TRUE/FALSE.
An example: IIF(include,RGB(248,254,14),RGB(255,255,255)) --> If field 'Include' is True, color YELLOW, if is False, color WHITE

Good Luck,
JRB-Bldr


 
Tore has given you the answer. You need to set the column's Sparse property to .F.

The way to remember which way round it goes is to know that "sparse" means exactly what it says. If a column is "sparse" (the Sparse property set to .T.), you will only see the desired effect in a few of the rows (in fact, in one row: the one that currently has the focus).

Since that's not what you want in this case, set the property to .F.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thank you tbleken, Olaf, Jrb and Mike for your very prompt replies and suggestions.

As I mentioned, I had already set the DynamicBackColor of Column5 to a different background Colour for ‘eligible’ and ‘ineligible’ rows of the grid; I believe that Jrb recommends adopting the same approach. This means that the user can see, (on the whole grid) which the ‘eligible’ rows are. Also, by setting the Readonly property of Column5 using a similar rule (in the AfterRowColChange method), that ensures that only in ‘eligible’, rows can the user enter data into Column5.

The problem however remains, that when the user navigates to a cell (using Uparrow, DnArrow), the cell is highlighted in light blue, whether or not the row is eligible. For ineligible rows (where I have set Readonly to be .T.), I would like the background colour of the cell to be grey, when it has focus. At present it has the default background colour, which (on my system at any rate) is light blue.

I have tried setting HighlightBackColor of the grid to a different value, but the cell remains light blue when it has focus. This remains the case even if I reset all the Dynamic properties to their defaults.

Setting Column5.Sparse = .F. does not achieve the intended result. I must be doing something wrong!

Grateful for any suggestion if anyone has been able to set the background colour of a cell in a grid when it has focus. A worked example would be particularly helpful.

Thanks. Andrew
 
OK, that's the problem. The active cell is the active cell. There is no simple setting overriding it's styling when it's active.
Besides there are more Highlight properties than just Highlightbackcolor, look around in the properties window! The natural thing to set when you don't want ti is Highlight=.F., still each active cell has it's styling as any focused control has.

If you take a native grid and set Highlight=.F., HighlightRow=.F. you get no blue lines and no blue border.
The inner cell being blue just denotes the value is selected, most probably. And that's the coloring for that case and that's never overriding with any backstyling. The blue "highlight" is just the selection and it only comes up with a double click or a single click on the place of the text cursor.

What control is in this cell, anway? Textbox or something else?

Bye, Olaf.
 
Thanks Olaf. Yes, the item in Column 5 is a textbox.

If I have mygrid.highlight = .T. (the default), then the current cell is highlighted backcolor light blue. I do not believe that the colour can be altered in this case.

If I have mygrid.highlight = .F. (thanks for the suggestion), then the backcolor when the cell is selected appears to taken from Column5.backcolor. I can alter this in the AfterRowColChange() event and achieve the effect I want (different backcolor if the user is not allowed to alter the cell value). That almost works, except it has the unfortunate side-effect that empty records at the bottom of the grid (after the last record from the record source) then pick up the Column5.backcolor in sympathy.

I have also tried setting mygrid.highlightbackcolor to (say) red, but this does not appear to have any effect on the colour when the cell is selected (with mygrid.highlight = .T.); the cell remains blue.

I think I may have to give up on this; again, thank you for the effort you have put into it.

Andrew
 
I believe you're referring to "Selected Items color". This is a OS (Windows) setting.
In Windows 7 (can't find this option in Windows 10) :
Control Panel -> Personalization -> Window Color -> Selected Items (chosen from the combobox)
On my system is RGB(51,153,255)

Unfortunately this setting affects many items, not only the grid (you can see the effect in Properties Window of VFP's IDE).

The VFP code to change that color is extremely short (successfully tested under Windows 7 and Windows 10)

Code:
Declare integer SetSysColors in user32 integer, string, string
lnNewColor = 255
setsyscolors(1,BINTOC(13,'4RS'),BINTOC(m.lnNewColor,'4RS')) && 1 means a single color would be changed; 13 is the numeric code for Selected Item

The same color setting affects the selected cell (when you only navigate) and the thin border around the selected row.
But if you change the color, after the form was created, the thin border remains unchanged, only the cell backcolor is changed. Awkward...

Anyway, this is a demo (calling those lines in the AfterRowColChange event)
Code:
PUBLIC ofrm
LOCAL lnOldColor
* Set the color to RED
lnOldColor = SelectedItemsColor(RGB(255,0,0))
* Do Form
ofrm = CREATEOBJECT("MyForm")
* pass the color to form
ofrm.lnOldColor = m.lnOldColor
ofrm.show()

FUNCTION SelectedItemsColor
	LPARAMETERS lnNewColor
	Declare integer GetSysColor in user32 integer
	Declare integer SetSysColors in user32 integer, string, string
	LOCAL lnOldColor
	lnOldColor = GetSysColor(13) && store the old color
	setsyscolors(1,BINTOC(13,'4RS'),BINTOC(m.lnNewColor,'4RS')) && set new color
	RETURN m.lnOldColor
ENDPROC

DEFINE CLASS myform as Form
	lnOldColor = 0
	ADD OBJECT grd as grid WITH recordsource = "cc"
	PROCEDURE load
		CREATE CURSOR cc (nF1 I AUTOINC,cF2 C(10) DEFAULT SYS(2015),lF3 L DEFAULT RECNO() % 3 = 0)
		FOR lni = 1 TO 100
			APPEND BLANK 
		ENDFOR
		GO TOP
	ENDPROC
	PROCEDURE Init
		This.grd.Column1.DynamicBackColor = "IIF(RECNO() % 2 = 0,RGB(255,200,200), RGB(255,255,255))"
		This.grd.Column2.DynamicBackColor = "IIF(lF3,RGB(200,225,200), RGB(255,255,255))"
		This.grd.Column2.Sparse = .F.
	ENDPROC
	PROCEDURE grd.afterrowcolchange
		LPARAMETERS nColIndex
		IF cc.lF3
			SelectedItemsColor(RGB(200,200,200))
		ELSE
			SelectedItemsColor(RGB(255,0,0))
		ENDIF
	ENDPROC
* restore the old color
	PROCEDURE Unload
		setsyscolors(1,BINTOC(13,'4RS'),BINTOC(This.lnOldColor,'4RS')) 
	ENDPROC
ENDDEFINE


Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Yes, I also already said it's the typical color of a selection. As you have a textbox it's often enough fully selected right away without the intention to do so.
To verify and to see that simply type or move the cursor, so either the selection is replaced by what you type or is removed, as you move the text cursor. It's not the cell color it's the selection color.

Bye, Olaf.
 
Thank you very much Wilhelm, for your careful answer to my question.

I have checked out the example you provided, and it exactly achieves the result - of setting the background colour of a selected cell in a grid; I am grateful for the trouble you have taken.

It happens that I have now examined an alternative approach, by setting the background colour in Column5.text1.gotfocus(). That also appears to do the trick. However I am sure that the facility you have provided will come in useful.

Sincerely. Andrew Mozley
 
I haven't tested, but if changing the cells textbox backcolor solves your problem, it must already have been set before, eg you've used some textbox other than the native one or have set the backcolor at design time. What's for sure is when using a grid with the default textboxes in it's columns the backcolor is white, if not overridden by either the dynamicbackcolor or highlightbackcolor and the settings I tried to best turn off all the highlighting were putting it to white, unless you doulbe clicked and thereby selected the text.

Anyway, it's good you found a solution.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top