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

StringGrid: right justify column

Status
Not open for further replies.

JulianUK

Programmer
Apr 17, 2002
73
GB
Hi

I'm trying to acheive a simple right justification on text on a column in a stringgrid on a form.

by default, they are all left justified.

how can I do this?

Many thanks

Julian
 
It is fairly easy to right justify text in a string 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
 
Thanks Andrew

I almost beat you to it! i managed to almost figure that in the end, but for a few details. That finishes it off for me nicely thanks.

Simple solution. nice.

As with most things, through searching around fo that solution I've found all sorts of other fun things to play with too...!

Ta.
Julian.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top