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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I right justify text in a StringGrid?

Graphical User Interface

How do I right justify text in a StringGrid?

by  towerbase  Posted    (Edited  )
It is fairly easy to right justify text in a String Grid (a similar technique can be used in a Draw Grid).

First you need to set the string grid DefaultDrawing property to False.

Second you need to write a handler for the string grid's OnDrawCell event.

The event handler should look something like:

Code:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  text: string;
  WidthOfText: integer;
  WidthOfCell: integer;
  LeftOffset: integer;
begin
 with StringGrid1 do begin
  text := Cells[ACol,ARow];
  WidthOfText := Canvas.TextWidth(text);
  WidthOfCell := ColWidths[ACol];
  LeftOffset := WidthOfCell - WidthOfText;
  Canvas.TextRect(Rect,Rect.Left+LeftOffset,Rect.Top,text);
 end;
end;

You may wish to enhance this code by changing the brush and pen properties of the string grid's canvas to introduce colour.

You may also wish to centre the text vertically in the cell in which case you will find the TextHeight function useful.

Andrew
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top