Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

StringGrid

I want to highlight certain cells in a string grid so I toggle font.color, the problem is that the whole grid color is set to the color of the last cell.
That's the whole grid, even cells written to before the color is toggled, ands even in the fixed Row 0.
What am I doing wrong?

0
frog
Asked:
frog
  • 2
1 Solution
 
vladhCommented:
frog,

try this:

procedure TForm1.DemoGridDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  dx : integer;
begin
  With (Sender as TStringGrid) do
  begin
  // Show Text Right-aligned, Text rechtsbundig setzen
    if (ARow = 0) and
       (Acol = 0) then
    With Canvas do
    begin
      FillRect(Rect);
      Text := cells[Acol,Arow];
      dx := TextWidth(Text) + 2;
      TextOut(Rect.Right-dx,Rect.Top,Text);
    end;

  // Set the Color, Feld-Farbe setzen
    case aCol of
      0 : canvas.brush.color := clBlack;
      1 : canvas.brush.color := clMaroon;
      2 : canvas.brush.color := clGreen;
      3 : canvas.brush.color := clOlive;
      4 : canvas.brush.color := clNavy;
    end;
    canvas.textrect (rect,rect.left+2,rect.top+2,cells[acol,arow]);
  end;
end;


 
code courtesy DelphiSwissCenter - nice collection of useful tips there.

http://www.swissdelphicenter.ch/en

Vlad
0
 
eshipmanCommented:
What is the criteria for highlighting the cells?

That is the key to making sure that the other cells
do not get highlighted in the code above.
0
 
frogAuthor Commented:
Thanks for your replies vladh and eshipman.
Sorry for not replying earlier, I've had to put my efforts elsewhere for a while.

The grid is only 6 columns wide and holds strings of only a word or two per cell mainly entered at run time. Cells with  entries out of bounds and some cells in one column calculated at run time need to be highlighted to indicate any cells needing attention such as an empty cell, a cell with an invalid date, etc.
It's such a small task that I decided to use a string grid.

I seem to continually come up against this same type of problem, ie expecting objects to be 'fully functional' instead of the 'half baked' things that Delphi seems to contain.
Nothing meant against you personally vladh, but your solution almost duplicates the whole string grid in terms of writing into and highlighting cells.
The grid can already highlight individual cells and even whole rows when they receive focus from the mouse, .... so all I want to know is the syntax to do it.
Is it ...
grid.highlight := clBlue;
cells[c, r].highlight := true;
Or perhjaps it's cells[c, r].lightup  .. etc .... I've tried all the combinations I can think of.
The properties and methods to highlight cells are already a part of the internal workings of the grid so I just want to access them, not duplicate them.


0
 
frogAuthor Commented:
Thanks vladh, sorry for the delay I've had a few distractions.


0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now