Recent

Author Topic: [SOLVED] How can I align the strings to center in each cells in StringGrid ?  (Read 20040 times)

gjpneac

  • New Member
  • *
  • Posts: 16
Hello,

procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid1.DefaultTextStyle.Alignment:= taCenter;
end;
   
--> unit1.pas(34,31) Error: Argument can't be assigned to

Lazarus 1.1 - 2012-07-09 - FPC 2.6.1 - SVN 37902 - i386-win32

Regards.
« Last Edit: July 17, 2012, 04:18:10 pm by gjpneac »

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: How can I align the strings to center in each cells in StringGrid ?
« Reply #1 on: July 17, 2012, 03:35:24 pm »
try ->
Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
var
  s:TTextStyle;
begin
  s := StringGrid1.DefaultTextStyle;
  s.Alignment:=taCenter;
  StringGrid1.DefaultTextStyle := s;
end;

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: How can I align the strings to center in each cells in StringGrid ?
« Reply #2 on: July 17, 2012, 03:45:29 pm »
Or, if you want to modify Alignment and/or other properties only for particular cells, use the OnPrepareCanvas event of the stringrid, like this:

Code: [Select]
procedure TForm1.GridPrepareCanvas(sender: TObject; aCol, aRow: Integer; aState: TGridDrawState);
var
  ts: TTextStyle;
begin
  if ARow=0 then
    Grid.Canvas.Font.Style := [fsBold];        // bold for header row
  if (ACol > 0) then begin                     // centered alignment for all data cells
    ts := Grid.Canvas.TextStyle;
    ts.Alignment := taCenter;
    Grid.Canvas.TextStyle := ts;
    if Grid.Cells[ACol, ARow] = '00:00' then   // not interesting cells in silver
      Grid.Canvas.Font.Color := clSilver;
  end;
end; 

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: How can I align the strings to center in each cells in StringGrid ?
« Reply #3 on: July 17, 2012, 04:12:59 pm »
At Design-Time, you can use the 'Columns Editor' and set each Column Alignment to taCenter or how ever you want it.  The Title is separate so if you want that centered, you have to set it as well.
Lazarus Trunk / fpc 2.6.2 / Win32

gjpneac

  • New Member
  • *
  • Posts: 16
Thanks. it work's fine.

@Avishai :  FixedCols := 1; So I suppose that Columns has no effect on Cols[0].

 

TinyPortal © 2005-2018