This post teaches you how to get the effect shown in the image below using the GRID component of visual fox pro.
Requires visual foxpro 9.0 SP2
I usually create a class for this, but here in the example I will do it in the simplest way, however, using a method inside the form.
You need to create a blank form, add a GRID, a REFRESH button and add a table. In my example I used a very basic table, called VALORES.DBF, with the VALOR field. If the value is 1, I use one color, if it is 2, I use another and so on.
My VALORES.DBF table is in the same directory where I am saving the FORM.
Creating the table
CREATE TABLE values (n(10,2) value)
INSERT INTO values (value) VALUES (1)
INSERT INTO values (value) VALUES (2)
INSERT INTO values (value) VALUES (3)
FORM PROPERTIES
BINDCONTROLS = .F.
DATASESSION = 1
INIT
SELECT values
LOCATE
THISFORM.BINDCONTROLS = .T.
LOAD METHOD
IF NOT FILE('values.dbf')
SET MESSAGE TO
MESSAGEBOX('Table VALORES.DBF is not available in '+SYS(5)+SYS(2003)+'.',16,'Notice')
THISFORM.RELEASE
RETURN (.F.)
ENDIF
IF NOT USED('values')
USE values IN 0 ALIAS values
ENDIF
CHANGE_COR_COLUMN METHODDO CASE
CASE values.value=1
RETURN(RGB(255,160,122))
CASE values.value =2
RETURN(RGB(242,214,0))
CASE values.value =3
RETURN(RGB(195,119,224))
OTHERWISE
RETURN(RGB(100,100,100) )
ENDCASE
Implementing this in the GRIDEdit the VALUE column, go to the DYNAMICBACKCOLOR property and put THISFORM.TROCAR_COR_COLUNA()
Gif showing: Click here to download
Download example: Click here to download
Download example: Click here to download
Implementation via code at Autocom3
Within Autocom3's ERP, implementation is not manual, as I showed above. It is done programmatically and elements are created at runtime. So, I make use of the GRID, initially set to -1 in the COLUMNCOUNT property and add columns, heads and components from an XML that contains all the grid definitions.
Tip: In the code above there is the implementation to change the default TEXTBOX of VFP for a TEXTBOX of a personal class. See that it is necessary to REMOVE the component from the VFP, then add the TEXTBOX of the class and set the CONTROLSOURCE again. "This part was a lot of work to discover, because I didn't find any documentation about it and it's a recurring question on the forums. I used the _crs variable to store the previous controlsource and then assign its value to the controlsource of the new TEXTBOX component I added.
The principle is the same, but more structured and the assignment will be done according to the line below, assigning the command to the dynamicbackcolor property and calling a method inside the FORM.
Let's go to dynamicbackcolor:
In my case I used an IF so that only columns that satisfy this condition have the dynamicbackcolor command filled. Note that it needs to be enclosed in "" quotes, as its assignment will be programmatically.
Another way would be to use the command below, but it would not suit my case, as I needed each cell to be highlighted in a different way and by COLUMN. The command below sets the entire line.
.SETALL("DynamicBackColor","thisform.TROCAR_COR_COLUNA()")
Comments
Post a Comment