-2

Im working on a project in Delphi , I have TShellListView component (List) ,and Button to create New folder :

MkDir(List.RootFolder.PathName+'\New Folder');
List.Update;

But what I need is when the user create the new folder, then then the folder automatically show in edit mode, so he can change the folder name, as when you create New Folder in Windows.

How can I do that?

Ilyes
  • 14,640
  • 4
  • 29
  • 55

2 Answers2

1

Try something like this:

var
  Path, PathName: string;
  Folder: TShellFolder;
  I: Integer;
begin
  Path := IncludeTrailingPathDelimiter(List.RootFolder.PathName) + 'New Folder';
  if not CreateDir(Path) then Exit;
  List.Refresh;
  for I := 0 to List.Items.Count-1 do
  begin
    Folder := List.Folders[I];
    if (Folder <> nil) and (Folder.PathName = Path) then
    begin
      List.Items[I].EditCaption;
      Exit;
    end;
  end;
end;

Alternatively:

var
  Path: string;
  Item: TListItem;
begin
  Path := IncludeTrailingPathDelimiter(List.RootFolder.PathName) + 'New Folder';
  if not CreateDir(Path) then Exit;
  List.Refresh;
  Item := List.FindCaption(0, 'New Folder', False, True, False);
  if Item <> nil then
    Item.EditCaption;
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I got an Error Type `String` and `Integer` on line , `Item := List.FindCaption('New Folder');` You miss the integer param for `FindCaption` – Ilyes Jan 14 '17 at 12:41
  • Should be like : `FindCaption(Integer,'New Folder',boolean , boolean, boolean);` , you pass just the string param. – Ilyes Jan 14 '17 at 12:48
  • I have fixed it – Remy Lebeau Jan 14 '17 at 17:54
  • Thank you , but 1st you miss again , remove `:` and add `;` in line `Item := List.FindCaption(0, 'New Folder', False, True, False):` , 2nd True this w'll create the folder but w'll not edit it. I mean `Item.EditCaption;` not working. – Ilyes Jan 14 '17 at 18:06
  • The True in the 4th parameter (`Inclusive`) tells `FindCaption()` to include the list item at the specified `StartIndex` (0) in the search. – Remy Lebeau Jan 14 '17 at 19:12
  • I have tested both solutions now and they both work. The problem is that `List.Update()` does not repopulate the ListView, so `'New Folder'` is not added to the list so it can then be found. You have to use `List.Refresh()` instead. I have updated my answer. – Remy Lebeau Jan 14 '17 at 19:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133165/discussion-between-sami-and-remy-lebeau). – Ilyes Jan 14 '17 at 20:12
0

I found a solution:

MkDir(List.RootFolder.PathName+'\New Folder');
List.Update;
List.ItemIndex:=0;
List.HideSelection:=True;
while List.ItemIndex<List.Items.Count-1 do
begin
  // Find the New Folder 
  if List.SelectedFolder.PathName=(List.RootFolder.PathName+ '\New Folder') then
  begin
    //Set the Folder in Edit mode & exit the loop
    List.Items[List.ItemIndex].EditCaption;
    Exit;
  end
  else
    //Inc the Index
    List.ItemIndex := List.ItemIndex+1;
end;
List.HideSelection:=False;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Ilyes
  • 14,640
  • 4
  • 29
  • 55
  • Why are you using `List.ItemIndex` and `List.SelectedFolder`? You should be able to loop through `List.Items[]` without changing the current selection. – Remy Lebeau Jan 14 '17 at 00:02