2017-01-13 38 views
-1

IM在Delphi中的一个项目工作,我有TShellListView组件(列表),以及按钮来创建新的文件夹:TShellListView创建新的文件夹和重命名为

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

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

我该怎么做?

回答

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; 
+0

我在线获得了错误类型'字符串'和'整数',Item = List.FindCaption('New Folder');''你错过了'FindCaption'的整数参数 – Sami

+0

应该是这样的:'FindCaption(Integer, '新建文件夹',布尔值,布尔值,布尔值);',你只传递字符串参数。 – Sami

+0

我已经修复了它 –

0

我找到了一个解决方案:

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; 
+0

为什么你使用'List.ItemIndex'和'List.SelectedFolder '?你应该能够在不改变当前选择的情况下遍历List.Items []'。 –

相关问题