delphi TShellListView创建新文件夹并重命名

6jygbczu  于 12个月前  发布在  Shell
关注(0)|答案(2)|浏览(133)

我在 Delphi 的一个项目工作,我有TShellListView组件(列表),按钮来创建新的文件夹:

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

但我需要的是当用户创建新的文件夹,然后文件夹自动显示在编辑模式,所以他可以改变文件夹名称,当你在Windows中创建新文件夹。
我怎么能这么做呢?

px9o7tmv

px9o7tmv1#

试试这样的方法:

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;

或者:

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;
h43kikqp

h43kikqp2#

我找到了一个解决办法:

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;

相关问题