unit UTree; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, Menus, StdCtrls; type PMyRec = ^TMyRec; TMyRec = record Name: string; //区域名称 Position: string; //所属位置 level:string; //地区级别 end; type TFTree = class(TForm) TreeView1: TTreeView; PopupMenu1: TPopupMenu; Add: TMenuItem; AddSub: TMenuItem; Delete: TMenuItem; Edit: TMenuItem; Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; Label1: TLabel; Label2: TLabel; Label3: TLabel; Save: TMenuItem; BSave: TButton; procedure AddClick(Sender: TObject); procedure AddSubClick(Sender: TObject); procedure DeleteClick(Sender: TObject); procedure EditClick(Sender: TObject); procedure TreeView1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure LoadData(MyRecPtr:PMyRec); procedure SaveData(MyRecPtr:PMyRec); procedure SaveClick(Sender: TObject); private { Private declarations } public { Public declarations } end; var FTree: TFTree; NodeNumber:integer; implementation {$R *.dfm} //将三个编辑框组件中的文本保存到特定结点的相关对象中 procedure TFTree.SaveData(MyRecPtr:PMyRec); begin MyRecPtr^.Name:=Edit1.Text; MyRecPtr^.Position:=Edit2.Text; MyRecPtr^.level:=Edit3.Text; end; //将特定结点的相关数据显示在三个编辑框组件中 procedure TFTree.LoadData(MyRecPtr:PMyRec); begin Edit1.Text :=MyRecPtr^.Name; Edit2.Text :=MyRecPtr^.Position; Edit3.Text :=MyRecPtr^.level; end; procedure TFTree.FormCreate(Sender: TObject); var MyRecPtr: PMyRec; begin Label1.Caption :='名称'; Label2.Caption :='所属区域'; Label3.Caption :='级别'; BSave.Caption :='保存'; New(MyRecPtr); MyRecPtr^.Name := '中国'; MyRecPtr^.Position := '亚洲'; MyRecPtr^.level := '国家级'; //添加根结点的相关对象 TreeView1.Items.AddObject(nil, '中国' , MyRecPtr); LoadData(MyRecPtr); NodeNumber :=1; end; procedure TFTree.AddClick(Sender: TObject); var NewNode :TTreeNode; MyRecPtr: PMyRec; begin //设置新结点的相关数据 New(MyRecPtr); Edit1.Text :='新结点'+inttostr(NodeNumber) ; Edit2.Text :='未知'; Edit3.Text :='未知'; SaveData(MyRecPtr); NodeNumber:=NodeNumber+1; //添加当前结点的兄弟结点 with TreeView1 do begin if Items.Count = 0 then NewNode :=Items.AddObject(nil, Edit1.Text , MyRecPtr) else NewNode :=Items.AddObject(selected, Edit1.Text , MyRecPtr); end; NewNode.Selected :=true; end; procedure TFTree.AddSubClick(Sender: TObject); var NewNode :TTreeNode; MyRecPtr: PMyRec; begin //设置新结点的相关数据 New(MyRecPtr); Edit1.Text :='新结点'+inttostr(NodeNumber) ; Edit2.Text :='未知'; Edit3.Text :='未知'; SaveData(MyRecPtr); NodeNumber:=NodeNumber+1; //添加当前结点的子结点 with TreeView1 do begin if Items.Count = 0 then NewNode :=Items.AddChildObject(nil, Edit1.Text , MyRecPtr) else NewNode :=Items.AddChildObject(selected, Edit1.Text , MyRecPtr); end; NewNode.Selected :=true; end; procedure TFTree.DeleteClick(Sender: TObject); var MyRecPtr: PMyRec; begin if TreeView1.Selected=nil then exit; MyRecPtr := TreeView1.Selected.Data; //释放当前结点的相关数据 Dispose(MyRecPtr); //删除当前结点 TreeView1.Selected.Delete; //使编辑框组件显示新结点的相关数据 TreeView1Click(nil); end; procedure TFTree.EditClick(Sender: TObject); begin TreeView1.Selected.EditText; end; procedure TFTree.TreeView1Click(Sender: TObject); var MyRecPtr: PMyRec; begin if TreeView1.Selected<>nil then begin MyRecPtr:= TreeView1.Selected.Data; LoadData(MyRecPtr); end; end; procedure TFTree.SaveClick(Sender: TObject); var MyRecPtr:PMyRec; begin if TreeView1.Selected<>nil then begin MyRecPtr:= TreeView1.Selected.Data; SaveData(MyRecPtr); end; end; end.
区域管理
最新推荐文章于 2009-02-24 11:51:00 发布