Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Right Justifying on a TStringGrid

192 views
Skip to first unread message

Alejandro Castro

unread,
Apr 12, 2001, 4:13:44 PM4/12/01
to
Im trying to rigth justify some cells on a TString Grid, Im using the
onDrawCell Event but doesnt work, if I try to use the CELLS property for get
the value of the cell always contains a null string

How Can I do it ?

Thanks
Alejandro

--
Alejandro Castro Perez

COMPUTACION ALFRA SC
Cerro del Fortin 116-101
Col. Campestre Churubusco
04200 México, D.F.
Tel: 55 44 6014 y 55 44 3251
Lada sin Costo: 01 800 849 77 59
www.calfra.com.mx


Clay Shannon

unread,
Apr 12, 2001, 5:10:05 PM4/12/01
to
Adapted from Neil Rubenking's book "Delphi Programming Problem Solver":

In your DrawCell event:

var
X: Integer;
begin
with Sender as TStringGrid do
begin
SetTextAlign(Canvas.Handle, TA_RIGHT);
X := (Rect.Left+Rect.Right) div 2;
Canvas.TextRect(Rect, X, Rect.Top+2, Cells[ACol,
ARow]);
end;
end;

"Alejandro Castro" <cal...@mail.internet.com.mx> wrote in message
news:3ad60e0f_2@dnews...

Mauro Patiño

unread,
Apr 12, 2001, 8:43:45 PM4/12/01
to
Alejandro Castro wrote:
>
> Im trying to rigth justify some cells on a TString Grid, Im using the
> onDrawCell Event but doesnt work, if I try to use the CELLS property for get
> the value of the cell always contains a null string
>
> How Can I do it ?

Use the DrawText win32 API function. It has a parameter for alignment.

procedure TForm1.FormCreate(Sender: TObject);
var
c, r: integer;
begin
StringGrid1.ColCount := 3;
for r := 0 to StringGrid1.RowCount - 1 do
for c := 0 to StringGrid1.ColCount - 1 do
StringGrid1.Cells[c,r] := IntToStr(r)+':'+IntToStr(c);
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow:
Integer;
Rect: TRect; State: TGridDrawState);
var
Fmt: integer;
begin
StringGrid1.Canvas.FillRect(Rect);
{set alignment}
case ACol of
0: Fmt := DT_SINGLELINE or DT_VCENTER or DT_LEFT;
1: Fmt := DT_SINGLELINE or DT_VCENTER or DT_CENTER;
2: Fmt := DT_SINGLELINE or DT_VCENTER or DT_RIGHT;
end;

{shrink the rect in 2 pixels so the text is not on the grid line}
InflateRect(Rect, -2, 0);
DrawText(StringGrid1.Canvas.Handle,
PChar(StringGrid1.Cells[ACol,ARow]),
-1,
Rect,
Fmt);
end;

Visit http://www.govst.edu/users/gsmpati/delphi for a fancier example.

Alejandro Castro

unread,
Apr 12, 2001, 9:54:43 PM4/12/01
to
Im using D3

Is there a problem with TSTRINGGRID ?

When I call the onDrawCell event I receive allways the same col and row,
sometimes the content of cells[col,row] is empty, etc.. The event is
impredectible

Why? am I missing something ?

Thanks again
Alejandro

Alejandro Castro <cal...@mail.internet.com.mx> escribió en el mensaje de
noticias 3ad60e0f_2@dnews...

Thomas S.

unread,
Apr 13, 2001, 5:44:30 AM4/13/01
to
Have a look at:
http://www.swissdelphicenter.ch/en/showcode.php?id=422

HTH

tom

Alejandro Castro <cal...@mail.internet.com.mx> schrieb in im Newsbeitrag:
3ad60e0f_2@dnews...

Peter Below (TeamB)

unread,
Apr 13, 2001, 6:18:33 AM4/13/01
to
In article <3ad65dfb$1_1@dnews>, Alejandro Castro wrote:
> Is there a problem with TSTRINGGRID ?

No.

> When I call the onDrawCell event I receive allways the same col and row,
> sometimes the content of cells[col,row] is empty, etc.. The event is
> impredectible
> Why? am I missing something ?

Perhaps you are thrown into a curve by the fact that the two parameters of
the event that pass in the col and row of the cell to draw are called Col
and Row, the same names used for the Col and Row *property* of the
stringgrid. So if you use a With construct inside the handler like

with sender as TStringgrid do begin
S:= Cells[ col, row ];

you will refer to the grid properties, not to the parameters of the event
handler! This has been fixed in later Delphi versions by renaming the
parameters aCol and aRow, to resolve the conflict. In older Delphi versions
you have to rename the parameters yourself, or copy the parameters to local
variables with nonconflicting names.


Peter Below (TeamB) 10011...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Alejandro Castro

unread,
Apr 13, 2001, 1:47:06 PM4/13/01
to
Yes !!!

I changed the name of the parameters Row and Col and works !!! and the code
is very simple (joining the code of everybody)

with (Sender as TStringGrid) do begin
Canvas.FillRect(Rect);
SetTextAlign(Canvas.Handle,TA_RIGHT);
Canvas.TextOut(Rect.Right-2,Rect.Top+1,Cells[aCol,aRow]);
end;

But now I have a question, I tried to find the TA_RIGHT and SETTEXTALIGN on
everywhere on the help files but I cant find them , where are they?

Thanks again
Alejandro


Peter Below (TeamB) <10011...@compuXXserve.com> escribió en el mensaje de
noticias VA.00006db...@antispam.compuserve.com...

Clay Shannon

unread,
Apr 13, 2001, 2:11:09 PM4/13/01
to
See the Windows unit, as these are Windows API stuff.

"Alejandro Castro" <cal...@mail.internet.com.mx> wrote > But now I have a

Peter Below (TeamB)

unread,
Apr 16, 2001, 4:33:17 PM4/16/01
to
In article <3ad73d2c_2@dnews>, Alejandro Castro wrote:
> But now I have a question, I tried to find the TA_RIGHT and SETTEXTALIGN on
> everywhere on the help files but I cant find them , where are they?

You have to look in win32.hlp, the Windows API helpfile. If you just place the
caret onto the word to find in the editor and hit F1 it should take you to
that helpfile, if it has been installed properly. As far as i remember the API
helpfiles are an optional install item but included in the standard
installation.

0 new messages