Solved

How To Align Real Values inside a StringGrid

Posted on 2001-09-09
7
579 Views
Last Modified: 2010-04-06
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
Comment
Question by:yurrea
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 10

Expert Comment

by:Jacco
ID: 6469863
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
 

Author Comment

by:yurrea
ID: 6469878
Jacco

I'll check it out.

Yvann
0
 
LVL 2

Expert Comment

by:PeterLarsen
ID: 6469976
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Accepted Solution

by:
VSF earned 100 total points
ID: 6470547
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
 

Author Comment

by:yurrea
ID: 6478202
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
 
LVL 2

Expert Comment

by:PeterLarsen
ID: 6478664
No worries - you'r problem is solved - thats what counts !!
0
 
LVL 3

Expert Comment

by:VSF
ID: 6479235
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now