VirtualStringTree columns should adapt size when one column is hidden
Asked Answered
S

1

8

I have a VST with 3 columns that evenly take the available space.

  • I have set hoAutoSpring in Header.Options, and
  • all columns have Column[x].Option have coAutoSpring set.

Now I want to be able to hide the last column and maintain that the other columns evenly take the free space (a little bit like a control with alClient).

When I only set the column invisible (see below) the space that was taken by the column is then simply unused.

VST.Header.Columns[2].Options:=VST.Header.Columns[2].Options - [coVisible];

When I set Header.Options.hoAutoResize to True and set Header.AutoSizeIndex to 1, then the 2nd Column will take all the new space.

Is there a method to tell the columns to fill up the available space and resize evenly?

Screenshot:

enter image description here

Standley answered 14/11, 2013 at 13:5 Comment(3)
+1 alone for the picture. Very good first question. Welcome to SO!Salomo
It turns out that AutoFitColumns with smaAllColumns does not show the behaviour I expected. I deleted my answer.Salomo
@jpfollenius: Thanks! I'm positively surprised how quick I found high quality answers here. I think I'll stay here for a while :)Standley
S
1

Thanks to all for your very quick and high quality responses!

As it seems that there's no built in way to solve my problem i've coded that the following way (just in case somebody runs into a similar problem):

// Show/hide a column and spread the space on all other visible columns
//   so that the proportions remain the same (as if AutoSpring was used)
procedure ChangeColumnVisibility(Tree: TVirtualStringTree; Column: TColumnIndex;
  NewVisible: boolean);
var Col : TVirtualTreeColumn;
begin
     Col:=Tree.Header.Columns[Column];
     if not (NewVisible xor (coVisible in Col.Options)) then
        Exit;

     if not NewVisible then
     begin
          Col.Options:=Col.Options - [coVisible];
          Tree.Header.ResizeColumns(Col.Width, 0, Tree.Header.Columns.Count-1);
     end
     else
     begin
          Tree.Header.ResizeColumns(-Col.Width, 0, Tree.Header.Columns.Count-1);
          Col.Options:=Col.Options + [coVisible];
     end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
     ChangeColumnVisibility(VST, 2, not (coVisible in VST.Header.Columns[2].Options));
end;
Standley answered 14/11, 2013 at 15:20 Comment(3)
Have you tried to show the same column you hide this way ? By setting the "filler" column width manually you keep for that autosize column just a small space if you show it again.Grenadines
You are right. Header.Options.hoAutoResize must be false for this to work properly. (I've set Header.options.hoAutoSpring and I've set coAutoSpring for all Columns in my app)Standley
just stumbled over Tree.Header.ResizeColumns which made the code much clearer - edited the code in my posting above.Standley

© 2022 - 2024 — McMap. All rights reserved.