Solved

Saving treeview state

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now