Inthe---> save all treeview in INI file

Hello Inthe, question for you

save all treeview in INI file and load it back

Esk
eskAsked:
Who is Participating?
 
intheConnect With a Mentor Commented:
hi,
never seen code to save and load to ini file but this may be helpful:(dont know who wrote it possibly someone at ex.ex)

unit Treeutils;

interface

uses Classes, ComCtrls;

const

TabChar = #9;
EndOfLine = #13#10;
CommaChar = #5;

{[ Text + CommaChar + ImageIndex + CommaChar + SelectedIndex + CommaChar + StateIndex + CommaChar ]}

procedure TreeSaveToStream(Stream: TStream; Tree: TTreeView);
procedure TreeSaveToFile(const AFileName: string; Tree: TTreeView);

procedure TreeLoadFromStream(Stream: TStream; Tree: TTreeView);
procedure TreeLoadFromFile(const FileName: string; Tree: TTreeView);

implementation

uses SysUtils,Comstrs;

procedure TreeLoadFromStream(Stream: TStream; Tree: TTreeView);

function GetBufStart(Buffer: PChar; var Level: Integer): PChar;
begin
Level := 0;
while Buffer^ in [' ',TabChar] do
begin
Inc(Buffer);
Inc(Level);
end;
Result := Buffer;
end;

function GetItemImages(var AStr: String; var Image, Selected, State: Integer): String;
var
I, IPos, PrevPos, ICount, ILength: Integer;
Tmp : String;
begin
Image := 0;
Selected := 0;
State := -1;

ILength := Length(AStr);
IPos := Pos(CommaChar, AStr);
if IPos > 0 then
begin
Tmp := Copy(AStr, IPos, ILength);
Delete(AStr, IPos, ILength);
ILength := Length(Tmp);

ICount := 0;
PrevPos := 0;

for I := 1 to ILength do
begin
if Tmp[I] = CommaChar then
begin
if (ICount > 0) and (PrevPos > 0) then
begin
case ICount of
1: Image := StrToIntDef(Copy(Tmp,PrevPos + 1 ,I - PrevPos - 1), 0);
2: Selected := StrToIntDef(Copy(Tmp,PrevPos + 1 ,I - PrevPos - 1), 0);
3: State := StrToIntDef(Copy(Tmp,PrevPos + 1 ,I - PrevPos - 1), -1);
end;
end;

Inc(ICount);
PrevPos := I;
end;
end;

end;

Result := AStr;
end;

var
List: TStringList;
ANode, NextNode: TTreeNode;
ALevel, i: Integer;
CurrStr: string;
Im,Se,St: Integer;
begin

if (Stream = nil) or (Tree = nil) then Exit;

List := TStringList.Create;
Tree.Items.BeginUpdate;
try
try
Tree.Items.Clear;
List.LoadFromStream(Stream);
ANode := nil;
for i := 0 to List.Count - 1 do
begin
CurrStr := GetBufStart(PChar(List[i]), ALevel);
CurrStr := GetItemImages(CurrStr, Im, Se, St);
if ANode = nil then
begin
ANode := Tree.Items.AddChild(nil, CurrStr);
ANode.ImageIndex := Im;
ANode.SelectedIndex := Se;
ANode.StateIndex := St;
end
else if ANode.Level = ALevel then
begin
ANode := Tree.Items.AddChild(ANode.Parent, CurrStr);
ANode.ImageIndex := Im;
ANode.SelectedIndex := Se;
ANode.StateIndex := St;
end
else if ANode.Level = (ALevel - 1) then
begin
ANode := Tree.Items.AddChild(ANode, CurrStr);
ANode.ImageIndex := Im;
ANode.SelectedIndex := Se;
ANode.StateIndex := St;
end
else if ANode.Level > ALevel then
begin
NextNode := ANode.Parent;
while NextNode.Level > ALevel do
NextNode := NextNode.Parent;
ANode := Tree.Items.AddChild(NextNode.Parent, CurrStr);
ANode.ImageIndex := Im;
ANode.SelectedIndex := Se;
ANode.StateIndex := St;
end
else raise ETreeViewError.CreateFmt(sInvalidLevelEx, [ALevel, CurrStr]);

if Tree.Items.Count > 0 then
Tree.Items[0].Selected := True;
end;
finally
Tree.Items.EndUpdate;
List.Free;
end;
except
Tree.Invalidate; // force repaint on exception
raise;
end;
end;

procedure TreeLoadFromFile(const FileName: string; Tree: TTreeView);
var
Stream: TStream;
Mode: Word;
begin

if (Tree = nil) then Exit;

if FileExists(FileName) then Mode := fmOpenRead
else Mode := fmCreate;

Stream := TFileStream.Create(FileName, Mode);
try
TreeLoadFromStream(Stream, Tree);
finally
Stream.Free;
end;
end;


procedure TreeSaveToStream(Stream: TStream; Tree: TTreeView);
var
i: Integer;
ANode: TTreeNode;
NodeStr: string;
begin

if (Stream = nil) or (Tree = nil) then Exit;

if Tree.Items.Count > 0 then
begin
ANode := Tree.Items[0];
while ANode <> nil do
begin
NodeStr := '';
for i := 0 to ANode.Level - 1 do NodeStr := NodeStr + TabChar;
NodeStr := NodeStr + ANode.Text + CommaChar +
IntToStr(ANode.ImageIndex) + CommaChar +
IntToStr(ANode.SelectedIndex) + CommaChar +
IntToStr(ANode.StateIndex) + CommaChar + EndOfLine;
Stream.Write(Pointer(NodeStr)^, Length(NodeStr));
ANode := ANode.GetNext;
end;
end;
end;

procedure TreeSaveToFile(const AFileName: string; Tree: TTreeView);
var
Stream: TStream;
begin

if (Tree = nil) then Exit;

Stream := TFileStream.Create(AFileName, fmCreate);
try
TreeSaveToStream(Stream, Tree);
finally
Stream.Free;
end;
end;


end.
0
 
edeyCommented:
does it have to be an ini file? or could something like this help:


SaveToFile saves a the tree view to the file specified in FileName.

procedure SaveToFile(const FileName: string);

Description

Use the SaveToFile method to store tree view data to a text file. The nodes can later be reloaded from the file into a new tree view object using the LoadFromFile method.


GL
mike
0
 
eskAuthor Commented:
yes, thanks
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
gandalf_the_whiteCommented:
listening...
0
 
gideon00Commented:
I have a much simpler way, granted it would need a little fine tuning for your specific case. But here goes:
unit helpered1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls, IniFiles;

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
        I: integer;
        Settings: TIniFile;
        MyIniFile: string;
begin
MyIniFile := ExtractFilePath(Application.Exename) + 'output.ini';
Settings := TIniFile.Create(Myinifile);
  for I := 0 to (TreeView1.Items.Count -1 ) do
Settings.WriteString(treeview1.topitem.Text, IntToStr(i), TreeView1.Items.Item[I].Text);
end;
end.
0
 
gideon00Commented:
(the editbox isnt needed, I just added it for debugging and playing around with the code :) )
0
 
intheCommented:
hi
but how to load a empty tree from ini file?
i had a few goes with no luck ;-(
0
 
eskAuthor Commented:
yeh, now is your turn again "gideon00"

Esk
0
 
eskAuthor Commented:
do you have any code "gideon00's" ?
0
 
gideon00Commented:
but you rejected my answer *sniff*
0
 
eskAuthor Commented:
sorry, i give you the points if you can come with code that's load it back

Esk
0
All Courses

From novice to tech pro — start learning today.