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

DynamicBackColor Syntax and Usage 1

Status
Not open for further replies.

JRB-Bldr

Programmer
May 17, 2001
3,281
US
I have my Grid Column's DynamicBackColor partially working, but I could use some advice/clarification on getting the remainder to work.

I understand that the value of DynamicBackColor is a String which I guess is executed as a macro somewhere along the line.

Currently I have the Row background color changing as desired based on two possible 'trigger' conditions with the following code:
.DYNAMICBACKCOLOR = ;
'IIF(chk.flag, RGB(240,250,0), ;
IIF(chk.changed, RGB(253,255,213), RGB(255,255,255)))'

Now in addition to the above I also want to change the background color of an individual cell within that same row if it's column's ControlSource = 'Trigger' Field.

To do that I need to make a check of the Column's ControlSource and compare it to a known value within the CHK table.

To attempt to do that I expanded the above working code to the following:
.DYNAMICBACKCOLOR = ;
"IIF(chk.flag AND ALLTRIM(UPPER(chk.Flag_Fld)) == ALLTRIM(UPPER(" + mcFldName + ")), RGB(255,0,0),;
IIF(chk.flag, RGB(240,250,0),;
IIF(chk.changed, RGB(253,255,213), RGB(255,255,255))))"

The above code generates an error on execution
Expression is invalid. Use a valid
expression for DYNAMICBACKCOLOR
property.


This code is executed during the dynamic build of the Grid and the value of mcFldName is what is used to set the individual column's ControlSource during the build.

SELECT MyTable
mnFldCnt = AFIELDS(aryFlds)
THISFORM.GRID.COLUMNCOUNT = mnFldCnt
FOR i = 1 TO mnFldCnt
mcFldName = aryFlds(i,1)
mcControlSource = ALIAS() + "." + mcFldName
WITH THISFORM.GRID.COLUMNS
.CONTROLSOURCE = mcControlSource
.DYNAMICBACKCOLOR = ;
<etc., etc....>
WITH
ENDFOR

Can you see what it is about my Expression which is InValid?

Thanks,
JRB-Bldr
 
While I've been corrected that your syntax should work, I've found that it's not always a good idea to extend a string constant over multiple lines. Try this:
Code:
.DYNAMICBACKCOLOR = ;
    "IIF(chk.flag AND ALLTRIM(UPPER(chk.Flag_Fld)) == ALLTRIM(UPPER(" + mcFldName + ")), RGB(255,0,0),";
     + "IIF(chk.flag, RGB(240,250,0),";
     +   "IIF(chk.changed, RGB(253,255,213), RGB(255,255,255))))"
Rick

 
Rick - Thanks for your prompt reply.

To eliminate the continuation of a string onto new lines, I have modified the code as follows:
.DYNAMICBACKCOLOR = ;
"IIF(chk.flag AND ALLTRIM(UPPER(chk.Flag_Fld)) == ALLTRIM(UPPER(" + mcFldName + ")), RGB(255,0,0),";
+ " IIF(chk.flag, RGB(240,250,0),";
+ " IIF(chk.changed, RGB(253,255,213), RGB(255,255,255))))"

But I am still getting the same Expression is invalid error message on execution.

My assigning this string expression to a test variable and looking at the results via the Command Window I have confirmed that it builds into a valid string that I expect.

Any other suggestions?

Thanks,
JRB-Bldr
 
Jrb-Bldr,

I suggest you start by putting the expression in a variable rather than storing it directly in the property. Then store the variable into the property and run the grid.

When you get the error, start gradually taking out bits of the expression until you no longer get the error. That'll tell you where the error occurs.

Also, keep in mind that the expression will be evaluated when the grid is initialised and also when it is refreshed. Make sure all the required variables are in scope at those two points.

Mike


Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
Through MUCH trial and error and MANY WAIT WINDOW Debug efforts, I found the problem and it was indeed a syntax error.

In addition to needing to change the mcFldName to mcControlSource, the primary problem was that I should have put the expression to the Right of the "==" within single quotes rather than no quotes around it.

.DYNAMICBACKCOLOR = ;
"IIF(chk.flag AND ALLTRIM(chk.Flag_Fld) == '" + mcControlSource + "', RGB(255,0,0),";
+ " IIF(chk.flag, RGB(240,250,0),";
+ " IIF(chk.changed, RGB(253,255,213), RGB(255,255,255))))"

The grid is now working and the appropriate fields within the FLAG row are now showing with a RED background.

Thanks for your help.
JRB-Bldr
 
JRB-Bldr,

Glad you got it working. Just for future reference -- and for the benefit of others who might be in this situation -- keep in mind that the expression that you pass to the DynamicXXX properties can consist of a call to a programmer-defined function.

In other words, you write a function which makes the decision about whether the property should be set or not. That function returns .T. or .F. The DynamicXXX property is simply a call to that function.

The advantage is that you can include any logic you like in the function, including IF/ELSE/ENDIF, etc. You are not confined to a single expression. It ought to make debugging a lot easier too.

Mike


Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
Mike -

Now that you have brought the subject up, I have some questions on how to do as you suggest...

If you reference a Function within the DynamicBackColor expression what is the syntax?

For a single string expression, it needs to be expressed as a STRING.
If instead of a single string expression you reference a Function, then is it too expressed as a STRING?

Example: DynamicBackColor = "ThisForm.MyBackColor()"

You indicate that the Function should return a .T. or .F., which might apply to some of the other DynamicXXXX properties, but isn't the DynamicBackColor looking for an RGB(nnn,nnn,nnn) value?

If assigning a value for DynamicBackColor, isn't that RGB(nnn,nnn,nnn) the value which should be passed back?

And, if so, should it not also be returned as a STRING (e.g. "RGB(nnn,nnn,nnn)" )?

Your clarification on this would be most appreciated.

Thanks,
JRB-Bldr
 
JRB-Bldr,

You indicate that the Function should return a .T. or .F., which might apply to some of the other DynamicXXXX properties, but isn't the DynamicBackColor looking for an RGB(nnn,nnn,nnn) value?

There's a simple explanation for that. I was talking rubbish.

You are correct when you say that the function should be returning an RGB value, or whatever other data type the Dynamic property is expecting.

For example, suppose you want to set the DynamicBackColor. You could do it like this:

THIS.Column1.DynamicBackColor = "MyBackColor()"
&& note the quotes

Then, elsewhere:

FUNCTION MyBackColor

IF <some condition>
RETURN RGB(255,255,0)
&& no quotes here, since the function is
&& returning a number
ELSE
RETURN RGB(128,128,128)
ENDIF

ENDFUNC

MyBackColor could also be a method, in which case you prefix it with the appropriate object reference, e.g.:

THIS.Column1.DynamicBackColor = "THISFORM.MyBackColor()"

You could also pass parameters to MyBackColor().

In the case of DynamicFontName, the function would return a string; for DynamicFontBold, it would return a logical, and so on.

I hope that answers your question.

Mike



Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
Hi

Although this is over 15 months old I wanted to give Dave a star. With help from the TypeBackColor UDF in Dave's FAQ I have made my grid rows different colours according to category and still kept the highlight bar.

The code is as follows:

Code:
*--- in the Form definition

PROCEDURE GetBackColor
   PARAMETERS mchkcolor
   DO CASE
     CASE recno(This.Grid1.RecordSource)=ThisForm.inRecno
        STORE ThisForm.gHighLight TO mbackcolor 
               &&... highlight colour RGB(...)
        RETURN &mbackcolor
     CASE mchkcolor = '   '
        RETURN RGB(255,0,255)	&&... white - no category
     OTHERWISE
        STORE trim(mchkcolor) TO mbackcolor 
               &&... colour code from record
       RETURN &mbackcolor
   ENDCASE
ENDPROC


*--- in the Init procedure of the Grid
PROCEDURE Init
   *... after all my column definitions etc
   This.SetAll("DynamicBackColor",
     "ThisForm.GetBackColor(myDBFName.ColorFieldName)",;
      "COLUMN")
ENDPROC

Big thanks to you Dave Summers sir


Kaz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top