【发布时间】:2017-01-13 17:16:12
【问题描述】:
我在 Delphi 做一个项目,我有 TShellListView 组件(列表)和按钮来创建新文件夹:
MkDir(List.RootFolder.PathName+'\New Folder');
List.Update;
但我需要的是当用户创建新文件夹时,然后文件夹会自动以编辑模式显示,这样他就可以更改文件夹名称,就像在 Windows 中创建新文件夹时一样。
我该怎么做?
【问题讨论】:
我在 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;
【讨论】:
String 和Integer,Item := 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) 处的列表项。
我找到了解决办法:
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.ItemIndex和List.SelectedFolder?您应该能够在不更改当前选择的情况下循环访问List.Items[]。