【问题标题】:TShellListView create new folder & rename itTShellListView 创建新文件夹并重命名
【发布时间】:2017-01-13 17:16:12
【问题描述】:

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

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

但我需要的是当用户创建新文件夹时,然后文件夹会自动以编辑模式显示,这样他就可以更改文件夹名称,就像在 Windows 中创建新文件夹时一样。

我该怎么做?

【问题讨论】:

    标签: delphi delphi-10-seattle


    【解决方案1】:

    试试这样的:

    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;
    

    【讨论】:

    • 我在网上遇到了错误类型StringIntegerItem := List.FindCaption('New Folder'); 你错过了FindCaption 的整数参数
    • 应该是:FindCaption(Integer,'New Folder',boolean , boolean, boolean);,你只传递字符串参数。
    • 我已经修好了
    • 谢谢,但第一次你又错过了,删除: 并在Item := List.FindCaption(0, 'New Folder', False, True, False): 行中添加;,第二次真的这将创建文件夹但不会编辑它。我的意思是Item.EditCaption; 不工作。
    • 第四个参数 (Inclusive) 中的 True 告诉 FindCaption() 在搜索中包含指定 StartIndex (0) 处的列表项。
    【解决方案2】:

    我找到了解决办法:

    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;
    

    【讨论】:

    • 你为什么使用List.ItemIndexList.SelectedFolder?您应该能够在不更改当前选择的情况下循环访问List.Items[]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 2018-08-17
    相关资源
    最近更新 更多