Arash
Arash

Reputation: 82

problem with a code for string grid ( align center )

Delphi: How to make cells' texts in TStringGrid center aligned?

When I use the top code (OnDraw part), it doesn't delete the first text and write the new text on the old text and one sel will duplicate .

Upvotes: 0

Views: 3027

Answers (1)

Ken White
Ken White

Reputation: 125757

You need to add a call to TCanvas.FillRect before writing out the new text:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  S: String;
begin
  S := StringGrid1.Cells[ACol, ARow];
  StringGrid1.Canvas.FillRect(Rect);
  SetTextAlign(StringGrid1.Canvas.Handle, TA_CENTER);
  StringGrid1.Canvas.TextRect(Rect,
    Rect.Left + (Rect.Right - Rect.Left) div 2, Rect.Top + 2, S);
end;

Note you'll also have to make sure that the TStringGrid.DefaultDrawing is set to False in order for this to work.

Upvotes: 2

Related Questions