Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


StringGrid colour problem

StringGrid colour problem

StringGrid colour problem

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);
 TempBack, TempFont : TColor;
  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
       TempBack := bkCol;       // Set to required background colour
       TempFont := TextCol;     // Set to required font colour
       Canvas.Brush.Color := TempBack;
       Canvas.font.Color := TempFont;
       Canvas.TextOut(Rect.Left+2,Rect.Top+2,Cells[ACol, ARow]); //Rewrite the text

The calling code has been amended by adding the colour setting code:

CODE -->

     // 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;    
     // the following code still works ok
     for l := i to (i+j)-1 do
       selectedCol := k;
       selectedRow := l;
       Grid1.cells[k,l] := sCode;     // Add booking code to cell

Can anyone point me in the right direction?


RE: StringGrid colour problem

Edit to above,
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.

RE: StringGrid colour problem

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

RE: StringGrid colour problem

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.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close