Solved

How To Align Real Values inside a StringGrid

Posted on 2001-09-09
7
594 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Suggested Courses
Course of the Month7 days, 12 hours left to enroll

632 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