Solved

Saving treeview state

Posted on 2008-09-30
5
735 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 250 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dev Express grid collapse 2 45
How to debug For loops? 3 49
Sending files from  idTcpServer Socket to idTcpClient 2 40
Tidtcpserver listening on multiports? 1 27
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

810 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