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!

StringGrid colour problem

Status
Not open for further replies.

rogerte

Programmer
Nov 9, 2001
164
GB
Many years ago (when Delphi 4 was new), we wrote a meeting room booking app, that allowed the reception people to load details of 6 room bookings into a paradox table, which assigned a booking code, room, start time and duration, and also type of booking (external customer, director, team etc).
The details are shown on a string grid set up with days across top, and time down left (each cell being 15 mins). Each cell displays the code of the meeting (calculated from the start time and meeting length) - this still works correctly.

Now, after 13 years, they want the cells coloured to represent the type of meeting!

I haven't touched Delphi in 9 years, and string grids since that was written in 1999!

However I can't get colouring to work :(

All I have done is to add some new TColor (for the colour), and integer (for the cell x and y co-ordinates) variables.

The grid now displays as before, except that the last cell entry, calculated for the last entry in the data table is displayed as a black cell (colour and font both black).

I have written the DrawCell procedure as below:
Code:
procedure TForm1.Grid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
 TempBack, TempFont : TColor;
begin
  TempBack := Canvas.Brush.Color;  //Save the current background colour
  TempFont := Canvas.font.Color;   // Save the current font colour
  if (ACol = SelectedCol) and (ARow = SelectedRow) then   // The cell is the one we want to change
       with TStringGrid(Sender) do
       begin
       TempBack := bkCol;       // Set to required background colour
       TempFont := TextCol;     // Set to required font colour
       Canvas.Brush.Color := TempBack;
       Canvas.font.Color := TempFont;
       Canvas.FillRect(Rect);
       Canvas.TextOut(Rect.Left+2,Rect.Top+2,Cells[ACol, ARow]); //Rewrite the text
       end;
end;

The calling code has been amended by adding the colour setting code:
Code:
[b]     // SetColours for meeting type
     if gType = 'GG' then  bkCol := clBlue
     else if gType = 'PS' then  bkCol:= clAqua
     else if gType = 'MT' then  bkCol:= clGreen
     else if gType = 'SQ' then  bkCol:= clMaroon
     else if gType = 'SA' then  bkCol:= clYellow;
     txtCol := clBlack;
     // make text visible on blue background 
     If Gtype ='GG' then txtCol := clWhite;[/b]    
     // the following code still works ok
     for l := i to (i+j)-1 do
       begin
       selectedCol := k;
       selectedRow := l;
       Grid1.cells[k,l] := sCode;     // Add booking code to cell
       end;

Can anyone point me in the right direction?

Thanks
 
Edit to above,
A
fter playing about with it, it looks like everytime the drawcell is called it resets every (non-fixed) cell to black text on white background except the last cell drawn which displays as I want it to. (the reason I mentioned it as black above was due to a typo in the code I hadn't spotted)

I was hoping that the assigning current values at the start of the drawcell would stop that happening.
 
I have done this some years ago, unfortunately it will be some time (a week) until I can get at the code!
What I do know is that I found that you have to make copies of ARow & ACol at the start of the procedure, as the values are (I think) renewed by interrupt in the background.

Steve: N.M.N.F.
If something is popular, it must be wrong: Mark Twain
 
Sorry for delay, but have had a few days holiday!

Managed to overcome it using the string grid Objects to hold the background and font TColors.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top