Recent

Author Topic: Column Autosize in TStringGrid?  (Read 1992 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1738
Column Autosize in TStringGrid?
« on: August 14, 2023, 05:22:12 am »
Recently I installed Lazarus 2.2.6. Are there any way that I can set auto column size in TStringGrid? I think I did this in the past, but cannot find the method any more.

Handoko

  • Hero Member
  • *****
  • Posts: 5515
  • My goal: build my own game engine using Lazarus
Re: Column Autosize in TStringGrid?
« Reply #1 on: August 14, 2023, 05:52:34 am »
I just tested TStringGrid.AutoSizeColumn(aCol) on my Lazarus 2.2.6 Linux, it worked:

https://lazarus-ccr.sourceforge.io/docs/lcl/grids/tcustomstringgrid.autosizecolumn.html

egsuh

  • Hero Member
  • *****
  • Posts: 1738
Re: Column Autosize in TStringGrid?
« Reply #2 on: August 14, 2023, 07:03:37 am »
Thank you Handoko.
It means I should run the program whenever I change something, doesn't it?

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: Column Autosize in TStringGrid?
« Reply #3 on: August 14, 2023, 07:11:40 am »
TCustomGrid.AutoFillColumns then?
https://lazarus-ccr.sourceforge.io/docs/lcl/grids/tcustomgrid.autofillcolumns.html

That property should do it automatically after changes.

Handoko

  • Hero Member
  • *****
  • Posts: 5515
  • My goal: build my own game engine using Lazarus
Re: Column Autosize in TStringGrid?
« Reply #4 on: August 14, 2023, 07:24:19 am »
I just tested AutoFillColumns, it behaved very similar to AutoSizeColumn:
- AutoFillColumns will resize the width of all columns
- AutoSizeColumn will only resize the width of the column supplied in the parameter
- Both AutoFillColumns and AutoSizeColumn won't autoresize if the data changed

If the data of the related column has been changed, we need to call the function again, they won't automatically resize it for you.

Tested only on Linux, not sure how will they behave on other OSes.
« Last Edit: August 14, 2023, 07:31:16 am by Handoko »

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: Column Autosize in TStringGrid?
« Reply #5 on: August 14, 2023, 07:31:01 am »
- Both AutoFillColumns and AutoSizeColumn won't autoresize if the data changed
Ha, that's strange and not according to documentation.
AutoFillColumns should do that.

Quote
Note that TGridColumn instances in Columns are initially created with SizePriority set to 1. For TDbGrid, which automatically populates Columns, this means the user will not be able to resize these columns using the mouse (since they are auto-sized).

AutoFillColumns is a property to set once and not a function to call every time.
You can't set it to true twice (you can but that shouldn't have any effect).

Handoko

  • Hero Member
  • *****
  • Posts: 5515
  • My goal: build my own game engine using Lazarus
Re: Column Autosize in TStringGrid?
« Reply #6 on: August 14, 2023, 07:41:40 am »
I retested AutoFillColumns again using this code:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Grids, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     StringGrid1: TStringGrid;
  17.     procedure Button1Click(Sender: TObject);
  18.     procedure FormCreate(Sender: TObject);
  19.   private
  20.   end;
  21.  
  22. var
  23.   Form1: TForm1;
  24.  
  25. implementation
  26.  
  27. {$R *.lfm}
  28.  
  29. { TForm1 }
  30.  
  31. procedure TForm1.FormCreate(Sender: TObject);
  32. begin
  33.   StringGrid1.Options := StringGrid1.Options + [goEditing];
  34. end;
  35.  
  36. procedure TForm1.Button1Click(Sender: TObject);
  37. begin
  38.   StringGrid1.AutoFillColumns := True;
  39. end;
  40.  
  41. end.

It seemed buggy on my test, or I just don't understand how it works? See the attached screenshot below, the columns weren't resize properly.

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: Column Autosize in TStringGrid?
« Reply #7 on: August 14, 2023, 08:48:08 am »
I retested AutoFillColumns again using this code:
Woops, sorry, I was wrong  :-[

AutoFillColumns is for autofilling the columns to the size of the grid so there is no whitespace next to the last column.
It will also hold this setting when the grid is resized (because of a right anker).

So calling StringGrid1.AutoSizeColumns after each change is the only way.

But I don't see an OnChange event.

egsuh

  • Hero Member
  • *****
  • Posts: 1738
Re: Column Autosize in TStringGrid?
« Reply #8 on: August 14, 2023, 11:30:43 am »
AutoFillColumns divide column sizes evenly to all columns. No?

With TDBGrid, I could set both column autosize and autofillcolumns by setting size priorityhy of the last column to 2.

Zvoni

  • Hero Member
  • *****
  • Posts: 3249
Re: Column Autosize in TStringGrid?
« Reply #9 on: August 14, 2023, 02:10:27 pm »
But I don't see an OnChange event.
OnEditingDone?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: Column Autosize in TStringGrid?
« Reply #10 on: August 14, 2023, 02:39:35 pm »
But I don't see an OnChange event.
OnEditingDone?
I meant something that would also trigger if you change cells in code.

You could use some idle event but that would use a lot of resources.
I guess it should also just be done in code then.

Zvoni

  • Hero Member
  • *****
  • Posts: 3249
Re: Column Autosize in TStringGrid?
« Reply #11 on: August 14, 2023, 03:09:35 pm »
But I don't see an OnChange event.
OnEditingDone?
I meant something that would also trigger if you change cells in code.
In that case you ARE already within "running" code. Fire off an AutoSizeColumn, and you're good.
OnEditingDone was more for if you change something in the Grid itself
The only other Event catching my eye is OnDrawCell or OnPaint
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018