我正在 Delphi 中开发一个项目,我有
TShellListView
组件(列表),以及用于创建新文件夹的按钮:
MkDir(List.RootFolder.PathName+'\New Folder');
List.Update;
但是我需要的是当用户创建新文件夹时,该文件夹会自动显示在编辑模式下,这样他就可以更改文件夹名称,就像在 Windows 中创建新文件夹一样。
我该怎么做?
尝试这样的事情:
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;
我找到了解决方案:
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.update 和 list.refresh 但没有达到所需的效果,对于它有效的现有文件夹。最终在表单上放置一个计时器(10毫秒),并在创建文件夹期间将其打开timer.enable。计时器事件处理编辑标题并自行切换。