Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TStringGrid.Cells vs #13+#10

Status
Not open for further replies.

rcloutie

Programmer
May 26, 2005
164
CA
Hi there,

I would like to add a 2-lines header (FixedRows) in a TStringGrid...
Tried to assign Cells[x, 0] := 'Price' + #13+#10 + 'USD'...
without results...

Any ideas?
 
You'd think that Delphi would be able to handle this automatically, but I never found a way to do it other than with a bit of code like this (see the OnDrawCell event of TStringGrid):
Code:
[COLOR=green]
{ Procedure to draw text centered in a given TCanvas cell.  If the text contains a CRLF, then two lines are drawn otherwise only one line is drawn.}[/color]
procedure CenterTwoLineText(AGrid:TStringGrid; Rect:TRect; ACol,ARow:integer;
        TextColor:TColor=clWindowText; Background:TColor=clWindow );
var
  zSaveForegroundColor,zSaveBackgroundColor:TColor;
  sText:string;
  nPos:integer;
  sLine1:string;
  sLine2:string;
  nHorCenter:integer;
  nVertCenter:integer;
  nTextWidth:integer;
  nTextHeight:integer;
  X:integer;
  Y:integer;
  Rect2:TRect;
begin
  zSaveForegroundColor := AGrid.Canvas.Font.Color;
  zSaveBackgroundColor := AGrid.Canvas.Brush.Color;
  try
    if Background <> clWindow then AGrid.Canvas.Brush.Color := Background;
    if TextColor <> clWindowText then AGrid.Canvas.Font.Color := TextColor;
    nHorCenter := Rect.Left + (Rect.Right - Rect.Left) div 2;
    nVertCenter := Rect.Top + (Rect.Bottom - Rect.Top) div 2;
    sText := AGrid.Cells[ACol,ARow];
    nPos := Pos(CRLF,sText);
    if nPos > 0 then
      begin[COLOR=green]
        // Line 1[/color]
        sLine1 := Copy(sText,1,nPos-1);
        nTextWidth := AGrid.Canvas.TextWidth(sLine1);
        nTextHeight := AGrid.Canvas.TextHeight(sLine1);
        X := nHorCenter - (nTextWidth div 2);
        Y := nVertCenter - nTextHeight;
        AGrid.Canvas.TextRect(Rect,X,Y,sLine1);[COLOR=green]
        // Line 2[/color]
        sLine2 := Copy(sText,nPos+2,999);
        Rect2.Left := Rect.Left;
        Rect2.BottomRight := Rect.BottomRight;
        Rect2.Top := nVertCenter + 1;
        nTextWidth := AGrid.Canvas.TextWidth(sLine2);
        X := nHorCenter - (nTextWidth div 2);
        Y := nVertCenter;
        AGrid.Canvas.TextRect(Rect2,X,Y,sLine2);
      end
    else
      begin[COLOR=green]
        // Single line:[/color]
        nTextWidth := AGrid.Canvas.TextWidth(sText);
        nTextHeight := AGrid.Canvas.TextHeight(sText);
        X := nHorCenter - (nTextWidth div 2);
        Y := nVertCenter - (nTextHeight div 2) - 1;
        AGrid.Canvas.TextRect(Rect,X,Y,sText);
      end;
  finally
    AGrid.Canvas.Brush.Color := zSaveBackgroundColor;
    AGrid.Canvas.Font.Color  := zSaveForegroundColor;
  end;
end;
And here is a test routine:
Code:
procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid1.RowHeights[0] := 36;
  StringGrid1.Cells[1,0] := 'Price' + #13#10 + 'USD';
  StringGrid1.Cells[2,0] := 'Net';
end;

procedure TForm1.StringGrid1DrawCell(
                        Sender: TObject;
                        ACol, ARow: Integer;
                        Rect: TRect;
                        State: TGridDrawState);
begin
  if (ARow = 0) then
    CenterTwoLineText(StringGrid1, Rect, ACol, ARow );
end;
 
Tried to assign Cells[x, 0] := 'Price' + #13+#10 + 'USD'...

That line can be written like this:
Cells[x, 0] := 'Price'#13#10'USD';

Just so you know :)


[bobafett] BobbaFet [bobafett]

Code:
if not Programming = 'Severe Migraine' then
                       ShowMessage('Eureka!');
faq102-5352
 
Bobbafet:
So it does I never knew that, suppose it must be the preceding # that makes it work.

This doesn't work...
'Hello'chr(10)chr(13)'world'
requires the '+' operators.

[red]Support the GNBM You know it makes sense[/red]More on this at forum1091
Steve: Delphi a feersum engin indeed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top