Recent

Author Topic: TStringgrid Column Sizing  (Read 1465 times)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 554
TStringgrid Column Sizing
« on: July 04, 2023, 01:26:37 pm »
Hello,

is it possible to reproduce the Excel Column sizeing-behaviour with a Stringgrid ?

i cant get it done. If the Columns width gets over the grids width then there is some strange behaviour when resizing that column again.

and when i set "ScrollBars" to ssAutoBoth and  "AutoFillColumns" to true then no Horizontal Scrollbar gets Displayed.
Edit:
https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/24992
was this ever fixed ?

thanks for any help !
« Last Edit: July 04, 2023, 01:51:54 pm by Weitentaaal »

jamie

  • Hero Member
  • *****
  • Posts: 7420
Re: TStringgrid Column Sizing
« Reply #1 on: July 04, 2023, 03:03:54 pm »
Assuming you are using the COLOMNS property.

Code: Pascal  [Select][+][-]
  1. StringGrid1.Columns[1].SizePriority := 0;
  2.  

That sets it so you can resize column 1 with all  your other resistrictions.

Personally, I don't think you should need to use the Columns property to do this, there must be another way.


I wanted to add that I don't think you are going to see scrollbars either way if you are filling the grid.
« Last Edit: July 04, 2023, 03:05:29 pm by jamie »
The only true wisdom is knowing you know nothing

Zvoni

  • Hero Member
  • *****
  • Posts: 3195
Re: TStringgrid Column Sizing
« Reply #2 on: July 04, 2023, 03:10:13 pm »
Hello,
is it possible to reproduce the Excel Column sizeing-behaviour with a Stringgrid ?
You mean this "double-click on the border of the header, and it resizes automagically" in Excel?
I'd probably look at Canvas.TextWidth
In a nutshell: Run through that column and look for the highest value of Canvas.TextWidth.
In the ColumnHeader-DoubleClick event set the Width to that max. value incl. some margins
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

wp

  • Hero Member
  • *****
  • Posts: 13270
Re: TStringgrid Column Sizing
« Reply #3 on: July 04, 2023, 03:33:30 pm »
My observations on Windows:
- Drop a TStringGrid on the form, set Align to alClient.  Double-click on the grid and enter some dummy data into some cells.
- Probably the right edge of the right-most column is still inside the window. Reduce the width of the form: when the right side of the form hits the right-most column, a horizontal scrollbar appears, as expected.
- Turn on the option goColSizing.
- With the horizontal scrollbar visible, move the mouse in the header column bar close to the right edge of the form (but still inside). At some point, the cursor will change to a "resize" cursor - now you can drag and resize the last column even when its physical right edge is not visible.
- Turn on the option goDblClickAutoSize.
- Move the mouse in the header bar above the column divider between two columns so that the cursor become the "resize" type again. Double-click, and the left column (as seen from the cursor position) auto-sizes its width. Note that the correct spot is a bit hard to find, and it may require several attempts for this to work.

So, in total: Very similar to Excel, I guess.

Weitentaaal

  • Hero Member
  • *****
  • Posts: 554
Re: TStringgrid Column Sizing
« Reply #4 on: July 04, 2023, 03:38:53 pm »
Hello,
is it possible to reproduce the Excel Column sizeing-behaviour with a Stringgrid ?
You mean this "double-click on the border of the header, and it resizes automagically" in Excel?
I'd probably look at Canvas.TextWidth
In a nutshell: Run through that column and look for the highest value of Canvas.TextWidth.
In the ColumnHeader-DoubleClick event set the Width to that max. value incl. some margins

no i meant the normal drag and drop to resize a column width.
I have problems achieving the following:

I want the columns to use up the whole width of the Control (-> AutoFillColumns: True)
I want the uSer to be able to resize the Columns (-> options:goColSizing:Checked)
I want Scrollbars when needed (i just found that i can use "ssBoth" instead of "ssAutoBoth" at the "Scrollbars" section)

it kinda works when i enable those options, but other Options get blocked (or it seems like it). I want to have a min Col width so that the Colwidth can't be 0 or less.
Solved that like this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid2HeaderSized(Sender: TObject; IsColumn: Boolean;
  2.    Index: Integer);
  3. begin
  4.    if IsColumn then begin
  5.       if StringGrid2.Columns[Index].Width < 20 then
  6.          StringGrid2.Columns[Index].Width:= 20;
  7.    end;
  8. end;
  9.  
  10. procedure TForm1.StringGrid2HeaderSizing(sender: TObject;
  11.    const IsColumn: boolean; const aIndex, aSize: Integer);
  12. begin
  13.    if IsColumn then begin
  14.       if aSize < 20 then
  15.          StringGrid2.Columns[aIndex].Width:= 20;
  16.    end;
  17. end;
  18.  

Everything works, except for the Case when i make a Column exceed the Grids Width. Resizing it then gives me a strange behaviour. I can't make a Video but the Problem is easy to recreate.

Edit: i checked why it feels that way and when resizing it does scroll at the same time horizontaly. Can i somehow disable Scrolling while resizing ?

Eit2: When i push a Column out of the controls width and the Column is the Last, then i can't see its contents anymore
« Last Edit: July 04, 2023, 03:45:12 pm by Weitentaaal »

wp

  • Hero Member
  • *****
  • Posts: 13270
Re: TStringgrid Column Sizing
« Reply #5 on: July 04, 2023, 05:06:07 pm »
I want the columns to use up the whole width of the Control (-> AutoFillColumns: True)
All columns having SizePriority=0 keep their assigned width. The remaining width of the grid is divided equally between all columns having SizePriority <> 0 (I think the individual non-zero values of SizePriority are not distinguished, in fact this feature would have been clearer if SizePriority would have been replaced by a boolean property "AutoSizeColumn").

Only the columns with SizePriority=0 can be actively resized by the user (by dragging the column at the right side of its header). But since due to resizing of such a column the remaining grid width changes, and the other columns (those with SizePriority<>0) will change their size as well. A bit confusing user experience. In particular because the mouse position gets out of sync with the dragged header side. Another annoying feature is that the resizing cursor appears also for the SizePriority<>0 columns which cannot be resized by dragging in AutoFillColumns mode.

I want Scrollbars when needed (i just found that i can use "ssBoth" instead of "ssAutoBoth" at the "Scrollbars" section)
This requirement contradicts the requirements for AutoFillColumns. When AutoFillColumns = true the entire width of the grid is filled by columns, and scrollbars cannot appear by definition. Well - normally. A horizontal scroll can appear neverthless, when a MinSize has been assigned to each column with SizePriority<> 0 and the total width of MinSize plus the current width of the SizePriority=0 column(s) is greater then the grid width.

When i push a Column out of the controls width and the Column is the Last, then i can't see its contents anymore
Move the mouse to the very edge of the right-most column header and drag it to the left. This may move in the lost column again.

Weitentaaal

  • Hero Member
  • *****
  • Posts: 554
Re: TStringgrid Column Sizing
« Reply #6 on: July 05, 2023, 07:50:44 am »
Quote
A bit confusing user experience. In particular because the mouse position gets out of sync with the dragged header side

thats the problem i tried to describe  :-[ .

All of my Columns are set to SizePriority = 0.

I will make the Grid bigger and wont let the Columns combined width get bigger than the Controls width.

 

TinyPortal © 2005-2018