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

Dynamicbackcolor for same items

Status
Not open for further replies.

rabbit75

Programmer
Aug 15, 2002
30
Here is something that would seem to be useful but I don't know if it is possible. I have a table of items at location, where the same item can be at many different locations. Is there a way to use dynamicbackcolor if I put the table in a grid (in item order) and I want all records of the same item to be the same color, alternating 2 colors?
(i.e. item A with be yellow and has 4 records, item B will be white and has 2 records, etc.)
Thanks
 
it would work if you have the correct logic in the iif statements for the dynamicbackcolor. you would have to have a extra field in the grid source that provides a identifier for the iif, and it would need nested iif's.

let me know how it goes Attitude is Everything
 
You can use any "formula" for deciding the dynamicbackcolor you want in a UDF. You just have to come up with the rule(s) you want to use, and they must only use the data available in the row itself to decide the color for that row's column.

Your statement, "... records of the same item to be the same color, alternating 2 colors" is a bit confusing, so I can't suggest some specific code. Note: If your example means that Items C would be yellow and D would be white, then you'll need to set a column flag before you display these records. The dynamicbackcolor can't "look back" at the previous record to decide what it's color should be - at least not without really slowing down the grid.

Rick
 
Rabbit75

Yes, you can, but you have to have a field that tells the different locations?

You can do it if you follow the example shown in FAQ184-1939 Mike Gagnon
 
rgbean,

By my statement "... records of the same item to be the same color, alternating 2 colors", I am trying to say, for example: I have 3 items, A, B and C. Item A is in 4 locations, B is in 2 and C is in 8. I want the table in item order. I want all records of Item A to be one color, say yellow. I want all records of Item B to be another color, say white. All the records for Item C would then be yellow and Item D would be white etc. I didn't think it would be possible to do this because of the need to 'look back' at the previous record for the item.

Thanks
 
mgagnon,

That won't work well as not all items exist at all locations, so there is no way to tell when the last record for that item is.

Thanks,
rabbit75
 
Rabbit75

The following might give you an idea how to do it:
Code:
oForm=CREATEOBJECT("myForm")
oForm.ADDOBJECT('myGrid1','myGrid')
oForm.myGrid1.column1.header1.CAPTION = "Name"
oForm.myGrid1.column2.header1.CAPTION = "Color"
oForm.myGrid1.column2.SPARSE = .F.
oForm.myGrid1.column2.ADDOBJECT("TextRed1","TextRed")
oForm.myGrid1.column2.ADDOBJECT("TextBlue1","Textblue")
oForm.myGrid1.column2.ADDOBJECT("TextYellow1","TextYellow")
oForm.myGrid1.column2.ADDOBJECT("Textgreen1","Textgreen")
oForm.SHOW(1)



DEFINE CLASS myForm AS FORM
    AUTOCENTER = .T.
    PROCEDURE LOAD()
        CREATE CURSOR myCursor (NAME C(20),LOCATION C(10))
        INSERT INTO myCursor (NAME,LOCATION) VALUES ("Widget1","bin1")
        INSERT INTO myCursor (NAME,LOCATION) VALUES ("Widget2","bin2")
        INSERT INTO myCursor (NAME,LOCATION) VALUES ("Widget3","bin4")
        INSERT INTO myCursor (NAME,LOCATION) VALUES ("Widget3","bin4")
        INSERT INTO myCursor (NAME,LOCATION) VALUES ("Widget3","bin4")
        INSERT INTO myCursor (NAME,LOCATION) VALUES ("Widget3","bin4")
        GO TOP
    ENDPROC
ENDDEFINE

DEFINE CLASS myGrid AS GRID
    VISIBLE = .T.
    COLUMNCOUNT = 2
    WIDTH = 200
    TOP = 20
    LEFT = 20
    PROCEDURE INIT()
        WITH THIS
            .column2.DYNAMICCURRENTCONTROL = "dynamic()"
        ENDWITH
    ENDPROC
ENDDEFINE
DEFINE CLASS TextRed AS TEXTBOX
    BACKcolor = RGB(255,0,0)
    VISIBLE = .T.
ENDDEFINE
DEFINE CLASS TextBlue AS TEXTBOX
    BACKCOLOR = RGB(0,128,255)
    VISIBLE = .T.
ENDDEFINE
DEFINE CLASS TextYellow AS TEXTBOX
    BACKCOLOR = RGB(255,255,0)
    VISIBLE = .T.
ENDDEFINE
DEFINE CLASS TextGREEN AS TEXTBOX
    BACKCOLOR=RGB(0,128,0)
    VISIBLE = .T.
ENDDEFINE

FUNCTION DYNAMIC
    DO CASE
        CASE myCursor.LOCATION = "bin1"
            RETURN "textRed1"
        CASE myCursor.LOCATION = "bin2"
            RETURN "textBlue1"
        CASE myCursor.LOCATION = "bin3"
            RETURN "textYellow1"
        CASE myCursor.LOCATION = "bin4"
            RETURN "textGREEN1"
    ENDCASE
ENDFUNC
Mike Gagnon
 
Mike,
It wouldn't have to be that complicated, but what if the "location" had thousands of possibilities or more importantly if the user could create new locations. I now believe he just wants to alternate the colors by groups (as opposed to every other record - like "greenbar" paper). If the grid is only showing a subset of the database (like a selected item category), then it'd be easy to preprocess the cursor and add a "color" column. Then a simple scan of the cursor comparing the current record's location to the previous record and setting the "color" based on the change.
Code:
SELECT mycursor
GO TOP
lnColor = rgb(255,255,255) && White
lcItem = mycursor.item && save it
REPLACE mycursor.itemcolor = lnColor
SCAN REST
   IF NOT(mycursor.item == lcItem)
      lcItem = mycursor.item && update saved
      * toggle the color
      IF lnColor = rgb(255,255,255) && White
         lnColor = rgb(255,255,127) && yellow
      ELSE
         lnColor = rgb(255,255,255) && White
      ENDIF
   ENDIF
   REPLACE mycursor.itemcolor = lnColor
ENDSCAN
GO TOP
Now the dynamicbackcolor can simply be set to mycursor.itemcolor.

Rick
 
mgagnon,

Thanks for the info, using a cursor is the way I would have gone too, but this a change to an existing program. My hands are tied as I can't make wholesale changes like that right now and the table is being used, not a cursor. I was hoping to be steered toward a way to do this using the table and not altering it.

rabbit75
 
Thanks for the info, using a cursor is the way I would have gone too, but this a change to an existing program. My hands are tied as I can't make wholesale changes like that right now and the table is being used, not a cursor. I was hoping to be steered toward a way to do this using the table and not altering it.

The cursor was only a "portable" example for you to try on your own system without me having to e-mail you an actual table. You can substitue a table (or a view) if you want.

Mike Gagnon
 
rgbean,

That is what I am looking for, but I was hoping to find a way without using a cursor and I cannot alter the table to add a column either.

rabbit75
 
HI Rabbit75,

Assumed.. myTable is the Grids RecordSource.

1. Add a form Property called.. cItemCode and initialise this with ""

2.MyGrid.InitEvent
******************
DODEFAULT()
WITH THIS
.SetAll("DynamicBackColor", ;
"IIF(myTable.ItemCode = ThisForm.cItemCode, ;
RGB(128,255,255),RGB (255,255,192))","COLUMN")
.SetAll("BackColor", RGB(255,192,192),"Header")
.SetAll("Alignment", 2, "Header")
** etc. whatever
ENDWITH

3. MyGrid.AfterRowColChangeEvent
********************************
ThisForm.cItemCode = IIF(EOF() OR BOF(),"",MyTable.ItemCode)
This.Columns(This.ActiveColumn).Text1.BackColor ;
= RGB(128,255,255)

Hope this helps you :) ramani :)
(Subramanian.G),FoxAcc, ramani_g@yahoo.com
 
Good one Ramani, I always forget about AfterRowColChange. However, will this really show the colors right on first display, on just as you change rows? (Yes I'm too lazy this afternoon to set up a test!)

Rick
 
Hi Rick
I am also shooting off my head.
You are right that the first display will not show the color. SO the solution is to set the value of ThisForm.cItemCode to the current record pointers value of the grids record source and then refresh the form. I think Rabbit should fix that. I have done similar coding and this shall not be a problem :)
ramani :)
(Subramanian.G),FoxAcc, ramani_g@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top