Solved

Saving treeview state

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

13 Experts available now in Live!

Get 1:1 Help Now