How can I make the TShellListView items appear ordered by name whenever its current directory has changed/its contents were updated (i.e., what is the proper/optimal place to call ShellListView1.Sort in)?
The TShellListView has a
SortOrder property; setting it to
stText sorts the items alphabetically (stData sort numerically by the Data pointer interpreted as numbers - not used by the standard TShellListView)
How can I attach a custom doubleclick event handler to items in TShellListView1? I would like to have a procedure that evaluates
the item (:=path in the filesystem) doubleclicked on, and if it happens to be a directory, navigate the associated TShellTreeView1 to that location in the FS.
Select the ShellListView, and click on the '...' next to the OnDblClick event in the event tab of the object inspector. Enter the following code to the generated code OnDblClick skeleton.
procedure TForm1.ShellListView1DblClick(Sender: TObject);
var
item: TShellListItem;
begin
item := TShellListItem(ShellListView1.Selected);
if item.IsFolder then
ShellTreeView1.Path := ShellListView1.GetPathFromItem(item);
end;
Explanation:
Normally the items in a TListView are type TListItem. TShellListView implements a descedant TShellListItem which has an field FileInfo containing the stored TSearchRec for each found file system item. TShellListView, therefore, can provide a method IsFolder telling whether this particular item is a folder or not. Either from the FileInfo or, for simpler, from the ShellListView's GetPathFromItem method you can determine the full path of the specified item; the selected item is accessible as ShellListview.Selected. And finally we assign this path to the Path property of the ShellTreeView which makes it switch to this path, open its nodes and communicates with the associated ShellListview to show the files.
What do I need to do so that I can have new controls on TForm1 align themselves similar to how TShellTreeView1 and TShellListView1 do? I would like an additional TGroupBox with some controls inside eventually, but when I add that to Form1 using the form designer, the existing controls do not contract/shrink in order to make some room for it (and I am not sure which control and/or property causes this behavior, unfortunately).
I suggest you create a new project to play with the Align properties of all controls:
- Drop a TPanel on the new form, set its Align to alLeft --> the panel will expand to cover the left part of the form.
- Add another panel, set its Align to alClient --> this panel will cover the rest of the form. When you resize the form, both panels will nicely fill the form, the width of the left panel will not change.
- If you want the user to be able to change the left panel width, add a splitter to the form. Since in the current state, however, the entire form is covered by panels, you very probably added the splitter to one of the panels rather than to the form: Go to the object tree above the object inspector, find the splitter's node and drag it immediately below the form's node. Now the splitter is a direct child of the form, but probably located at the very left of the form. Go to the form editor, select the splitter and drag it to the right of the left panel so that it snaps at its right side.
- In the same way you can add a right-(or top- or bottom-)aligned panel with Align set to alRight (or alTop or alBottom, respectively).
- And you can use other controls instead of the panels or place other controls into the panels where you again can use the Align property to position them.
In my opinion this is a simple possibility to achieve a "nice" layout. Another, more advanced, possibility is the Anchor Editor - read about it in
https://wiki.freepascal.org/Anchor_Sides.
How can I add or remove columns from the vsReport ViewStyle of ShellListView1? I would like to remove the "Type" column (displaying file extensions after the last "." in the entries' names) and add a column denoting whether or not the entry listed is a directory, normal file, special file, etc. Ideally, I would like to display a distinct graphical component (small icon) for each type - if that is even possible, while still maintaining the vsReport style?
Columns have not been in the focus of the ShellListView development... But you can add a new column after the ShellListview has been created, e.g. in the OnCreate event of the form:
var
MyColIndex: Integer;
procedure TForm1.Formcreate(Sender: TObject);
begin
...
with ShellListView1.Columns.Add do
begin
MyColIndex := Index; // Save the index of the column to access it later
Caption := 'Test';
Width := 80;
Alignment := taRightJustify;
end;
I think, you cannot delete the default columns, but you can hide them:
procedure TForm1.FormCreate(Sender: TObject);
begin
ShellTreeView1.Columns[2].Visible := false; // Column #2 is the "Type" column
The widths of the 3 default columns are maintained by the component, therefore, you may have to readjust column widths when you add or hide columns yourself.
In Windows, shell tree and listview items automatically get icons from the system. In Linux and Mac you must do this yourself: Add a TImagelist component to the form, doubleclick on it and add the icons that you need. Assign this imageList to the Images property of the ShellTreeView and the SmallImages property of the ShellTreeView (you need LargeImages fom a second image list also when you switch the ShellListView to ViewStyle vsIcon). Then, in the OnFileAdded event of the ShellListView, assign the requested image by its indes in the ImageList to the ImageIndex property of the provided Item. In the same way there is a OnGetImageIndex property for the ShellTreeView (and use the OnGetSelectedIndex to assign the image index to be used when a tree node is selected).