Link to home
Start Free TrialLog in
Avatar of yurrea
yurrea

asked on

How To Align Real Values inside a StringGrid

How do you right-align real values inside columns of a StringGrid using Tahoma as font? .. Right now I encounter problems because what I do is add spaces in front of the text to move it to the right .. however, it doesn't work quite well .. the values seem to be in zigzag mode .. Is there something wrong with what I do? or is it because of the font? .. especially when there are commas (',') in the value. Is there a way to get around this problem?

Yvann
Avatar of Jacco
Jacco
Flag of Netherlands image

Hi Yvann,

Yes the font is the problem if you use Courier as the font you wil not have this problem. You can either use a DrawGrid to overcome this problem (downside is that the drawgrid will not store data). Or you can use the OnDrawCell event of the TStringGrid.

In the OnDrawCell even you you can issue a Canvas.TextOut on the correct position. You can calculate the width of the string by calling Canvas.TextWidth(sYourString). And then use Rect.Right - iTextWidth - 3 as the X coordinate in the Canvas.TextOut call.

Regards Jacco
Avatar of yurrea
yurrea

ASKER

Jacco

I'll check it out.

Yvann
This should give you an idea.

var
 R1 : TRect;
begin
R1:=StringGrid1.CellRect(x, y)
DrawText(StringGrid1.Canvas.Handle, PChar(Text), -1, R1, DT_CALCRECT or DT_RIGHT or
 DT_WORDBREAK or DT_NOPREFIX or DrawTextBiDiModeFlagsReadingOnly);
end;

Regards
Peter
ASKER CERTIFIED SOLUTION
Avatar of VSF
VSF
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yurrea

ASKER

Jacco, PeterLarsen:

I can't seem to make your solutions work. :(
Sorry guys.


VSF:

This is what I'm looking for, and it works fine. :)
No worries - you'r problem is solved - thats what counts !!
I'm glad to help.

Please show your support visiting my homepages:

www.victory.hpg.com.br
www.enge.cbj.net

Thanks
VSF
UIN:14016999