Recent

Author Topic: [SOLVED] TShellListView column width in vsList mode  (Read 335 times)

Vittorio

  • New Member
  • *
  • Posts: 24
[SOLVED] TShellListView column width in vsList mode
« on: July 08, 2020, 09:48:29 am »
Dear colleagues,
I use ShellListView in vsList mode to display files connected with some projects.
Is there some way to change width (or amount) of columns in vsList mode because there are only 2 columns by default?
« Last Edit: July 14, 2020, 08:06:09 am by Vittorio »

wp

  • Hero Member
  • *****
  • Posts: 7552
Re: TShellListView column width in vsList mode
« Reply #1 on: July 08, 2020, 11:56:36 am »
I think the TShellListview was primarily designed to work in vsReport style. But here is a solution to get it working as you need:

The TShellListView inherits from TCustomListView a public property Column[index]: TListColumn. I thought that this property is for vsReport style only, but as i noticed the width of the first column is used also in vsList style. Since the ShellListview is designed with 3 columns by default I tried to set Column[0].Width to some reasonable value like 200. However, this did not work, the width was always autosizing to some value depending on the width of the control. The reason is in TCustomShellListView.Resize (I am removing the DEBUG instructrions and comments for simpler reading):

Code: Pascal  [Select][+][-]
  1. procedure TCustomShellListView.Resize;
  2. begin
  3.   inherited Resize;
  4.  
  5.   if Self.Columns.Count < 3 then Exit;
  6.  
  7.   if Width < 400 then
  8.   begin
  9.     Column[0].Width := (50 * Width) div 100;
  10.     Column[1].Width := (25 * Width) div 100;
  11.     Column[2].Width := (25 * Width) div 100;
  12.   end
  13.   else
  14.   begin
  15.     Column[0].Width := (70 * Width) div 100;
  16.     Column[1].Width := (15 * Width) div 100;
  17.     Column[2].Width := (15 * Width) div 100;
  18.   end;
  19. end;

As you can see the width of the three default columns is autocalculated as fractions of the control width. But, at the top, you can see that this does not happen when the number of columns is less than 3.

So, a simple, very hacky solution to your problem would be to delete one of the columns (but keep the first one) and set the width of the first column as you need. The following code gives the columns in vsList style a constant width of 200 pixels:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   ShellListview1.Columns.Delete(2);
  4.   ShellListView1.Column[0].Width := 200;
  5. end;

Of course, needless to say that you cannot return to vsReport style in this situation, you must recreate the deleted column before doing so. Therefore, I think a better solution would be to patch TCustomShellListView.Resize and exit when the viewstyle is not vrReport:

Code: Pascal  [Select][+][-]
  1. procedure TCustomShellListView.Resize;
  2. begin
  3.   inherited Resize;
  4.  
  5.   if (ViewStyle <> vrReport) or (Self.Columns.Count < 3) then Exit;
  6.  
  7.   ... // rest unchanged
  8. end;

With this solution you still can change ViewStyle and adjust the column width:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.StyleRadiobuttonChange(Sender: TObject);  
  2. begin
  3.   if Sender = rbListStyle then
  4.   begin
  5.     ShellListView1.ViewStyle := vsList;
  6.     ShellListView1.Column[0].Width := 200;
  7.   end else
  8.     ShellListView1.ViewStyle := vsReport;
  9. end;

Let me think about it for a while, before I apply this solution to Laz trunk. Please report back when you see any issues with this solution.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Vittorio

  • New Member
  • *
  • Posts: 24
Re: TShellListView column width in vsList mode
« Reply #2 on: July 09, 2020, 03:03:01 pm »
wp, thank you a lot for the irrefragable answer!
Both methods work well. The only misprint is 'vrReport'.

wp

  • Hero Member
  • *****
  • Posts: 7552
Re: TShellListView column width in vsList mode
« Reply #3 on: July 09, 2020, 04:55:07 pm »
I applied the changes in TCustomShellListView.Resize to Lazarus trunk.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

 

TinyPortal © 2005-2018