Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TFileSream with Treeview Data

Status
Not open for further replies.

pavement

Technical User
Nov 17, 2005
1
GB
I have a treeview set to add nodes only to the second level, each node has a name and description data object attached to it. i can get the tree structure to save, but cannot figure out how to get the data to save with it, please help.

----------------------------------

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
FormEx, ComCtrls, StdCtrls, TFlatTabControlUnit, TFlatComboBoxUnit,
Buttons, Menus;

type
TMainForm = class(TForm)
TreeView: TTreeView;
mView: TMemo;
BtnDel: TButton;
lblName: TLabel;
BtnPAdd: TBitBtn;
MainMenu1: TMainMenu;
procedure OnNodeChange(Sender: TObject; Node: TTreeNode);
procedure DelSelectedNode(Sender: TObject);
procedure AddNew(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;



type
PData = ^TData;
TData = record
Name : string;
Comments : string;
end;

var
MainForm: TMainForm;
TreeStream,DataStream: TFileStream;

implementation

uses unit2;

{$R *.DFM}

procedure TMainForm.OnNodeChange(Sender: TObject; Node: TTreeNode);
begin
if treeview.selected.level <> 0 then
begin
mView.Text := PData(TreeView.Selected.Data)^.Comments;
lblName.Caption := PData(TreeView.Selected.Data)^.Name;
end
end;

procedure TMainForm.DelSelectedNode(Sender: TObject);
begin
if treeview.selected.level <> 0 then
treeview.selected.free;
end;

procedure TMainForm.AddNew(Sender: TObject);
var
DataPtr: PData;
begin

AddNode := TAddNode.Create(Self);
AddNode.ShowModal;
AddNode.Free;

if status = 1 then
begin
New(DataPtr);
DataPtr^.Name := NewName;
DataPtr^.Comments := NewComments;
with treeview do
if selected.level <> 0 then
selected := selected.parent;
treeview.items.AddchildObjectfirst(treeview.selected, NewName, DataPtr);
treeview.fullexpand;
end
else
begin
mView.Text := '';
lblName.Caption := '';
end;

end;

procedure TMainForm.FormCreate(Sender: TObject);
var
F:TFileStream;
begin
F:=TFileStream.Create('TreeView.txt',fmOpenRead or fmShareDenyWrite);
F.ReadComponent(TreeView);
F.Free;
end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
var
F: TFileStream;
begin
F:=TFileStream.Create('TreeView.txt',fmCreate or fmShareCompat);
F.WriteComponent(TreeView);
F.Free;
end;
end.

----------------------------------

unit Unit2;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, TFlatComboBoxUnit;

type
TAddNode = class(TForm)
BtnOk: TBitBtn;
BtnCan: TBitBtn;
mComments: TMemo;
edName: TEdit;
FlatComboBox1: TFlatComboBox;
procedure BtnOkClick(Sender: TObject);
procedure BtnCanClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
AddNode: TAddNode;
NewName, NewComments: string;
Status: integer;

implementation

uses Unit1;

{$R *.DFM}

procedure TAddNode.BtnOkClick(Sender: TObject);
begin
Status := 1;
If (mComments <> Nil) and (edName <> Nil) then
begin
NewName := edName.text;
NewComments := mComments.text;
AddNode.Close;
end
else
begin
NewName := 'New';
NewComments := 'No Comments';
AddNode.Close;
end
end;

procedure TAddNode.BtnCanClick(Sender: TObject);
begin
AddNode.Close;
Status := 2
end;

end.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top