Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 607
  • Last Modified:

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
0
yurrea
Asked:
yurrea
  • 2
  • 2
  • 2
  • +1
1 Solution
 
JaccoCommented:
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
0
 
yurreaAuthor Commented:
Jacco

I'll check it out.

Yvann
0
 
PeterLarsenCommented:
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
VSFCommented:
This is how its done!
Hope this helps!
VSF
www.victory.hpg.com.br

ALLING CELLS IN A STRINGGRID
I need to allign cells in a stringgrid, first cell to the left ,second to the right and so on ...
Answer:
procedure WriteText(ACanvas: TCanvas; const ARect: TRect; DX, DY: Integer;
                    const Text: string; Format: Word);
var
  S: array[0..255] of Char;
  B, R: TRect;

begin
  with ACanvas, ARect do
  begin
     case Format of
        DT_LEFT   : ExtTextOut(Handle, Left + DX, Top + DY, ETO_OPAQUE or ETO_CLIPPED,
                               @ARect, StrPCopy(S, Text), Length(Text), nil);

        DT_RIGHT  : ExtTextOut(Handle, Right - TextWidth(Text) - 3, Top + DY,
                               ETO_OPAQUE or ETO_CLIPPED, @ARect, StrPCopy(S, Text),
                               Length(Text), nil);

        DT_CENTER : ExtTextOut(Handle, Left + (Right - Left - TextWidth(Text)) div 2,
                               Top + DY, ETO_OPAQUE or ETO_CLIPPED, @ARect,
                               StrPCopy(S, Text), Length(Text), nil);
     end;
  end;
end;


procedure TBEFStringGrid.DrawCell(Col, Row: Longint; Rect: TRect; State: TGridDrawState);
var

   procedure Display(const S: string; Alignment: TAlignment);
   const
      Formats: array[TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);
   begin
      WriteText(Canvas, Rect, 2, 2, S, Formats[Alignment]);
   end;

begin
   { test the Col and Row arguments here and format the cells as you want }

   case Row of
   0     : { Center column headings }
           if (Col < ColCount) then
              Display(Cells[Col,Row], taCenter)
   else
      { Right justify all other entries }
      Display(Cells[Col,Row], taRight);
   end;

end;
0
 
yurreaAuthor Commented:
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. :)
0
 
PeterLarsenCommented:
No worries - you'r problem is solved - thats what counts !!
0
 
VSFCommented:
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
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now