Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Saving treeview state

Posted on 2008-09-30
5
Medium Priority
?
792 Views
Last Modified: 2010-04-21
Is there any way to save the state of the ttreeview, by that I mean the collapsed/expanded state of a ttreeview's node(s) with subitems? Save it onclose of the form, and load it onactivate.
Thanks.
0
Comment
Question by:Cilician
  • 3
5 Comments
 
LVL 14

Expert Comment

by:SteveBay
ID: 22608435
There is not a direct and easy way to do this. You would need to make a list of expended nodes and store that list upon close and load that list upon startup. The list could be stored in the registry or an ini or a DB.
0
 
LVL 21

Accepted Solution

by:
ziolko earned 1000 total points
ID: 22608610
this should do the trick:

procedure TForm1.Button1Click(Sender: TObject);
var cnt: Integer;
    s: string;
begin
  for cnt := 0 to TreeView1.Items.Count - 1 do
    if TreeView1.Items[cnt].Expanded then
      s := s + '1,'
    else
      s := s + '0,';
    Memo1.Lines.Text := s;
end;

procedure TForm1.Button2Click(Sender: TObject);
var s: string;
    cnt, len: Integer;
begin
  s := Memo1.Lines.Text;
  len := Length(s) div 2;
  for cnt := 1 to len do
    if s[cnt * 2 - 1] = '1' then
      TreeView1.Items[cnt - 1].Expand(False)
    else
      TreeView1.Items[cnt - 1].Collapse(False)
end;

instead putting s to memo save it to file.
note that I skipped part that checks if there's enough nodes and if string was not manipulated

ziolko.
0
 

Author Closing Comment

by:Cilician
ID: 31501678
Great. Thanks.
0
 
LVL 21

Expert Comment

by:ziolko
ID: 22608706
this should be better:

uses Math;

procedure SaveTreeState(ATreeView: TTreeView;const AFileName: string);
var cnt: Integer;
    buff: array of Boolean;
    fs: TFileStream;
begin
  if Assigned(ATreeView) and (ATreeView.Items.Count > 0) then begin
    SetLength(buff, ATreeView.Items.Count);
    for cnt := 0 to ATreeView.Items.Count - 1 do
      buff[cnt] := ATreeView.Items[cnt].Expanded;
    fs := TFileStream.Create(AFileName, fmCreate or fmOpenWrite);
    try
      fs.Write(buff[0], ATreeView.Items.Count);
    finally
      fs.Free;
    end;
  end;
end;

procedure LoadTreeState(ATreeView: TTreeView;const AFileName: string);
var cnt: Integer;
    buff: array of Boolean;
    fs: TFileStream;
begin
  if Assigned(ATreeView) then begin
    fs := TFileStream.Create(AFileName, fmOpenRead);
    try
      SetLength(buff, fs.Size);
      fs.Read(buff[0], fs.Size);
      for cnt := 0 to Min(fs.Size, ATreeView.Items.Count) - 1 do
        if buff[cnt] then
          ATreeView.Items[cnt].Expand(False)
        else
          ATreeView.Items[cnt].Collapse(False);
    finally
      fs.Free;
    end;
  end;
end;

ziolko.
0
 
LVL 21

Expert Comment

by:ziolko
ID: 22608732
one more improvement:

procedure LoadTreeState(ATreeView: TTreeView;const AFileName: string);
var cnt: Integer;
    buff: array of Boolean;
    fs: TFileStream;
begin
  if Assigned(ATreeView) then begin
    fs := TFileStream.Create(AFileName, fmOpenRead);
    try
      if fs.Size > 0 then begin
        SetLength(buff, fs.Size);
        fs.Read(buff[0], fs.Size);
        for cnt := 0 to Min(fs.Size, ATreeView.Items.Count) - 1 do
          if buff[cnt] then
            ATreeView.Items[cnt].Expand(False)
          else
            ATreeView.Items[cnt].Collapse(False);
      end;
    finally
      fs.Free;
    end;
  end;
end;

ziolko.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question