Hi everyone

I have a simple yet annoying problem. I'm trying to change the selected color in a stringgrid, ie when selecting a cell the background must change color. The grid has no fixed rows or columns and only one column (like a simple list editor). I've overridden ondrawcell for the grid, but I can get only the first cell (or row in this case) to be drawn correctly. When I select the other cells only the background color changes and I cannot see the cell's text! When the cells are not selected their contents display perfectly. Defaultdrawing = true.
The grid is in a modal form, and I've also overridden the onkeydown of the grid to accept the enter key to add a new row to the grid and select it. So here is the code:

procedure TForm2.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  with sender as tstringgrid do
  begin
    if (gdselected in state) then 
    begin
      canvas.Brush.Color := clRed; //for illustrative purposes :-)
      canvas.Brush.Style := bsSolid;
      canvas.FillRect(rect);
      canvas.Font.Color := clWhite;
      canvas.TextRect(rect,1,1,cells[acol,arow]);
    end;
  end;
end;

procedure TForm2.StringGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (key = vk_return)     then
  begin
    stringgrid1.rowcount := stringgrid1.RowCount + 1;
    postmessage(stringgrid1.Handle,wm_keydown,vk_down,0);
  end;
end;

Thanx for any help!

Sorted! I passed the wrong parameters in

TextRect()

It should look like this:

TextRect(rect,[B]rect.left,rect.top[/B],cells[acol,arow]);

I thought the x,y offset was relative to passed rect parameter, how stupid of me!

:icon_redface:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.