[DUG]: Centering text in a cell in a tstringgrid.

John Bird johnkbird at paradise.net.nz
Mon Mar 20 17:06:21 NZST 2006


There are enhanced stringgrid components that have this built in, but you
can do it in the standard stringgrid (D5 and D2006).

Here is how I do a left or right justification in standard stringgrid.  Have
to turn the default drawing property off, as you are drawing the cell
yourself.  You can modify this to do centre.  As far as passing the key
press - if no other way works you could set a variable in your unit in the
key press event eg called CenterKeyPressed:Boolean

And in the drawcell event below modify it with a bit of centering code like

	if CenterKeyPressed then
	begin
		lhs := Rect.left + (Rect.right - Canvas.TextWidth(celltxt))
div 2
	end

(Note! I haven't tested that calculation)



procedure Form1.strgrd1DrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  lhs:Integer;
  celltxt:string;
begin
  with strgrd1 do
  begin
    canvas.brush.color:=clWhite;
    canvas.Font.color:=clNavy;
    if arow=row then				//current row
    begin
    	canvas.brush.color:=clHighlight;
        canvas.Font.color:=clwhite;
    end;
    Canvas.FillRect(Rect);
    celltxt := cells[Acol,Arow];
    lhs:=Rect.Left;
    //right justify columns 5 and 7
    if (Acol=5) or (Acol=7) then
    lhs := Rect.Right - Canvas.TextWidth(celltxt) - 5;
    Canvas.TextOut(lhs, Rect.Top, celltxt);
  end;
end;

John


-----Original Message-----
From: delphi-bounces at ns3.123.co.nz [mailto:delphi-bounces at ns3.123.co.nz] On
Behalf Of Chris Veale
Sent: Friday, 23 July 2004 1:44 p.m.
To: delphi at delphi.org.nz
Subject: [DUG]: Centering text in a cell in a tstringgrid.
Importance: High



** High Priority **

Hi.

Can someone please tell me if its possible to do a cell.align := center;

in a stringgrid please.

I cant see a property anywhere that will allow cell by cell alignment.

Cheers

Chris Veale

______________________________________________________

The contents of this e-mail are privileged and/or confidential to the named
recipient and are not to be used by any other person and/or organisation. If
you have received this e-mail in error, please notify 
the sender and delete all material pertaining to this e-mail.
______________________________________________________
_______________________________________________
Delphi mailing list
Delphi at ns3.123.co.nz http://ns3.123.co.nz/mailman/listinfo/delphi




More information about the Delphi mailing list