Board index » delphi » Right Justification of numeric strings in String Grids

Right Justification of numeric strings in String Grids

Does anyone know an easy way to right justify numbers in a string grid.  The solution must be editable.  
Therefore, padding the number with spaces won't work.

Any comments would be appreciated.

Blake

 

Re:Right Justification of numeric strings in String Grids


Quote
Blake Versiga (In-To...@ix.netcom.com) wrote:
> Does anyone know an easy way to right justify numbers in a string grid.  The solution must be editable.  
> Therefore, padding the number with spaces won't work.
> Any comments would be appreciated.
> Blake

        No, it works if you use a monospaced font. But I sure
        that there is a more elegant way to do this.

--
          o oooo o o
     oooo o o    oo           I                    r...@pinon.ccu.uniovi.es
     o    o o    o o     O====OIIIIIIIIIIIIII>      AVILES-ASTURIAS-SPAIN
     o    o oooo o  o         I

Re:Right Justification of numeric strings in String Grids


Quote
Blake Versiga <In-To...@ix.netcom.com> wrote:
>Does anyone know an easy way to right justify numbers in a string grid.  The solution must be editable.  
>Therefore, padding the number with spaces won't work.
>Any comments would be appreciated.
>Blake

I used the following code in a TDrawGrid onDrawCell event recently,
which should be similar to what you are trying to do with a string
grid except you get your data from the grids' own list.

Column 3 of the grid is an amount which is displayed right justified,
(but of course the inplace editor will still edit left justified when
selected for editing).
----------------------------------
procedure TClient.ClientGridDrawCell(Sender: TObject; Col,
  Row: Longint; Rect: TRect; State: TGridDrawState);
var
  Text : String;
 begin
    case Col of
    0:
      Text := Data[Row].Date;
    1:
      Text := Data[Row].Tag;
    2:
      Text := Data[Row].Cat;
    3:
      Text := FloatToStrF(Data[Row].Amt, ffCurrency, 10,2);
    end; { case }

  with Rect, ClientGrid.Canvas do
    if Row = 0 then
      { draw titles here (snipped) }
    else
    case Col of
    0,1,2:
      TextRect(Rect, Left + 2,Top + (Bottom - Top - TextHeight(Text))
div 2,
        Text);
    3:
      { ***** this is right justifying the amount column ***** }
      TextRect(Rect, Right - TextWidth(Text) - 2,
        Top + (Bottom - Top - TextHeight(Text)) div 2, Text);
    end;
end;
----------------------------------

--------------------------------------------------
Phil Wynn                      philw...@ihug.co.nz
PJ MicroElectronics Ltd.     CompuServe:100405,466
P.O. Box 534 Albany, NZ.

Other Threads