Link to home
Start Free TrialLog in
Avatar of faimo
faimo

asked on

treeview with text file

hi every body one i was looking for an answer to my question on how to save the treeview's nodes data into a  text file so i found an answer to the question asked by MOHFA : treeview and text file the anwser was done by krypto2000 :that was his anwser :
as ir was :
Hello,

I have look for the itemdata property but the problem is that is only a pointer and not "real data" so when I saveToFile there don't save...

So I have maked a new little application, it work like this :
   
   when you open the application, you can see the tree. If file exists it load from file, else create new file. okay.
   when you click on an item, in the edit you can see the description if exists. if not, you can add it.
   for adding description to item, switch to edit mode (there is a checkBox) and then, click on the item you want to add description,
   and you can see the item caption in the edit. Then, just add a delimiter (i have choose ">" you can change) and write your description after.
   Now, return to normal mode, click on the item and you can see the description in the edit !!!

   I hope that is that you want ;-)

   (that would be great if you have an e-mail where I can send to you the application because I am at work and the fu*** firewall block anything, no ftp possible)

//----------------------------------- FORM1 ----------------------------------------//
object Form1: TForm1
  Left = 192
  Top = 103
  Width = 292
  Height = 319
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  OnShow = FormShow
  PixelsPerInch = 96
  TextHeight = 13
  object TreeView1: TTreeView
    Left = 10
    Top = 10
    Width = 261
    Height = 241
    HideSelection = False
    Indent = 19
    TabOrder = 0
    OnClick = TreeView1Click
    OnCustomDrawItem = TreeView1CustomDrawItem
    Items.Data = {
      020000001F0000000000000000000000FFFFFFFFFFFFFFFF0000000002000000
      064974656D5F30220000000000000000000000FFFFFFFFFFFFFFFF0000000000
      000000097375624974656D5F30220000000000000000000000FFFFFFFFFFFFFF
      FF0000000000000000097375624974656D5F311F0000000000000000000000FF
      FFFFFFFFFFFFFF0000000001000000064974656D5F3122000000000000000000
      0000FFFFFFFFFFFFFFFF0000000000000000097375624974656D5F30}
  end
  object Edit1: TEdit
    Left = 10
    Top = 260
    Width = 186
    Height = 21
    TabOrder = 1
    OnChange = Edit1Change
  end
  object CheckBox1: TCheckBox
    Left = 205
    Top = 263
    Width = 71
    Height = 17
    Caption = 'Edit mode'
    TabOrder = 2
    OnClick = CheckBox1Click
  end
end
//-------------------------------------------------------------------------------------//

//----------------------------------- UNIT1----------------------------------------//
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Edit1: TEdit;
    CheckBox1: TCheckBox;
    procedure FormShow(Sender: TObject);
    procedure TreeView1CustomDrawItem(Sender: TCustomTreeView;
      Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
    procedure TreeView1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Desc: TStringList;

const
  FILE_NAME = 'C:\tree.txt';
  DELIMITER = '>';


implementation

{$R *.dfm}

procedure resetDesc(Desc: TStrings);
var i: integer;
begin
   Desc.Clear;
   for i := 0 to Form1.treeView1.Items.Count do
       Desc.Add('');
end;

procedure fileToTree(trv: TTreeView);
begin
   trv.LoadFromFile(FILE_NAME);
   trv.FullExpand;
end;

procedure treeToFile(trv: TTreeView);
begin
   trv.FullExpand;
   trv.SaveToFile(FILE_NAME);
end;

procedure TForm1.FormShow(Sender: TObject);
var strm: TStream; i: integer;
begin
   if fileExists(FILE_NAME) then
      fileToTree(TreeView1)
   else
      treeToFile(TreeView1);
   resetDesc(Desc);
end;

procedure TForm1.TreeView1CustomDrawItem(Sender: TCustomTreeView;
  Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);

function getDesc(s:string): string;
begin
  if pos(DELIMITER,s) > 0 then
     result := rightStr(s,length(s) - pos(DELIMITER,s))
else result := s;
end;

function getCaption(s:string): string;
begin
  if pos(DELIMITER,s) > 0 then begin
     result := leftStr(s,pos(DELIMITER,s)-1);
     Desc[Node.AbsoluteIndex] := getDesc(s);
     end
else result := s;
end;

begin
if not CheckBox1.Checked then
   Node.Text := getCaption(Node.Text);
end;

procedure TForm1.TreeView1Click(Sender: TObject);
begin
   if (treeView1.Selected <> nil) then
   if not CheckBox1.Checked then
      Edit1.Text := Desc[TreeView1.Selected.AbsoluteIndex]
   else
      Edit1.Text := TreeView1.Selected.Text;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
   Desc := TStringList.Create;
end;

procedure TForm1.CheckBox1Click(Sender: TObject);
begin
   resetDesc(Desc);
   fileToTree(TreeView1);
   Edit1.Clear;
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
   if treeView1.Selected <> nil then
      if CheckBox1.Checked then
         begin
         TreeView1.Selected.Text := Edit1.Text;
         treeToFile(TreeView1);
         end;
end;

end.
//-----------------------------------------------------------------------------------//

so my question is how can i replace the edit by a memo because the edit has one line of text whereas the memo  has a multi line , i want to store all the memo text ( i mean more that line of text used in the edit ) i tried by memo1.lines.text but doesn't work as i like .
could any one help me in replacing the code where the edit is declared ( in both node edit mode and node view mode )

thanks in advance
Avatar of Mike Littlewood
Mike Littlewood
Flag of United Kingdom of Great Britain and Northern Ireland image

does it make a difference if you just assign to memo1.text rather than memo1.lines.text?
Avatar of faimo
faimo

ASKER

yes it makes a big differance , i tried with the two : memo1.text and memo1.lines.text .
when i use memo1.lines.text it works but when use copy and past on this memo that's the problem . i don't know why
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial