如果每次打开后能自动读取上次展开的状态就会非常快捷
下载地址:
实现方法
将已经展开的节点索引 放在一个文本中
最后选中的那个节点索引放在最后一位
然后通过下面这个代码这实现
{
TreeView1.Items[0].Expand(False); //按节点索引 展开节点一级
TreeView1.Select(TreeView1.Items[1],[]);// 按节点索引 选中节点
}
TreeView1.Selected.AbsoluteIndex; //选中的节点索引号
{
SaveTreeViewExplandState(TreeView1,‘TreeView.txt‘);
}
procedure SaveTreeViewExplandState(TreeView: TTreeView; FileName: string);
var
List: TStringList;
i: Integer;
begin
List := TStringList.Create;
With TreeView do
begin
for i := 0 to Items.Count - 1 do
begin
if Items.Item[i].Expanded then
List.Add(IntToStr(i));
end;
List.Add(IntToStr(Selected.AbsoluteIndex));
end;
List.SaveToFile(FileName);
List.free;
end;
{
LoadTreeViewExplandState(TreeView1, ‘abc.txt‘);
}
procedure LoadTreeViewExplandState(TreeView: TTreeView; FileName: string);
var
List: TStringList;
i: Integer;
begin
List := TStringList.Create;
with List do
begin
List.LoadFromFile(FileName);
for i := 0 to Count - 2 do //最后一行放的是最后选中的那个节点索引
begin
TreeView.Items[StrToInt(List[i])].Expand(False);
end;
TreeView.Select(TreeView.Items[StrToInt(List[Count - 1])], []);
free;
end;
end;
{ TreeView1.Items[0].Expand(False); //按节点索引 展开节点一级 TreeView1.Select(TreeView1.Items[1],[]);// 按节点索引 选中节点 } TreeView1.Selected.AbsoluteIndex; //选中的节点索引号 保存TREEVIEW状态代码 { SaveTreeViewExplandState(TreeView1,‘TreeView.txt‘); } procedure SaveTreeViewExplandState(TreeView: TTreeView; FileName: string); var List: TStringList; i: Integer; begin List := TStringList.Create; With TreeView do begin for i := 0 to Items.Count - 1 do begin if Items.Item[i].Expanded then List.Add(IntToStr(i)); end; List.Add(IntToStr(Selected.AbsoluteIndex)); end; List.SaveToFile(FileName); List.free; end; 加载TREEVIEW状态代码 { LoadTreeViewExplandState(TreeView1, ‘abc.txt‘); } procedure LoadTreeViewExplandState(TreeView: TTreeView; FileName: string); var List: TStringList; i: Integer; begin List := TStringList.Create; with List do begin List.LoadFromFile(FileName); for i := 0 to Count - 2 do //最后一行放的是最后选中的那个节点索引 begin TreeView.Items[StrToInt(List[i])].Expand(False); end; TreeView.Select(TreeView.Items[StrToInt(List[Count - 1])], []); free; end; end;