程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> TTreeView講解 [7] - Insert、Delete

TTreeView講解 [7] - Insert、Delete

編輯:Delphi

本例效果圖:

TTreeView講解 [7] - Insert、Delete

unit Unit1; 
 
interface 
 
uses 
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
 Dialogs, ComCtrls, StdCtrls, Grids, ExtCtrls; 
 
type 
 TForm1 = class(TForm) 
  TreeView1: TTreeVIEw; 
  Button1: TButton; 
  Button2: TButton; 
  Button3: TButton; 
  Button4: TButton; 
  procedure Button1Click(Sender: TObject); 
  procedure Button2Click(Sender: TObject); 
  procedure Button3Click(Sender: TObject); 
  procedure Button4Click(Sender: TObject); 
  procedure Button5Click(Sender: TObject); 
  procedure FormCreate(Sender: TObject); 
 end; 
 
var 
 Form1: TForm1; 
 
implementation 
 
{$R *.dfm} 
 
procedure TForm1.FormCreate(Sender: TObject); 
var 
 Nodes: TTreeNodes; 
 node: TTreeNode; 
begin 
 TreeVIEw1.Align := alLeft; 
 Nodes := TreeVIEw1.Items; 
 node := Nodes.Add(nil, '一連長'); 
 node := Nodes.AddChild(node, '一排長'); 
 node := Nodes.AddChild(node, '一班長'); 
 node := Nodes.AddChild(node, '戰士1'); 
 Nodes.Add(node, '戰士2'); 
 Nodes.Add(node, '戰士3'); 
 Nodes.Add(node, '戰士4'); 
 node := node.Parent; 
 Nodes.Add(node, '二班長'); 
 Nodes.Add(node, '三班長'); 
 node := node.Parent; 
 Nodes.Add(node, '二排長'); 
 Nodes.Add(node, '三排長'); 
 node := node.Parent; 
 Nodes.Add(node, '二連長'); 
 Nodes.Add(node, '三連長'); 
end; 
 
{初始化} 
procedure TForm1.Button1Click(Sender: TObject); 
begin 
 TreeVIEw1.Items.Clear; 
 FormCreate(nil); 
 TButton(Sender).Caption := '初始化'; 
end; 
 
{刪除} 
procedure TForm1.Button2Click(Sender: TObject); 
var 
 node: TTreeNode; 
begin 
 if not Boolean(TreeView1.Items) then Exit; {如果 TreeVIEw1 無內容} 
 if TreeVIEw1.SelectionCount = 0 then Exit; {如果無選擇} 
 node := TreeVIEw1.Selected; 
 node.Delete; 
 
 TButton(Sender).Caption := '刪除'; 
end; 
 
{刪除它的所有下級} 
procedure TForm1.Button3Click(Sender: TObject); 
var 
 node: TTreeNode; 
begin 
 if not Boolean(TreeVIEw1.Items) then Exit; 
 if TreeVIEw1.SelectionCount = 0 then Exit; 
 node := TreeVIEw1.Selected; 
 if node.HasChildren then node.DeleteChildren 
 else ShowMessage(node.Text + ' 沒有下級'); 
 
 TButton(Sender).Caption := '刪除下級'; 
end; 
 
{用 TTreeNodes 刪除} 
procedure TForm1.Button4Click(Sender: TObject); 
var 
 node: TTreeNode; 
begin 
 if not Boolean(TreeVIEw1.Items) then Exit; 
 if TreeVIEw1.SelectionCount = 0 then Exit; 
 node := TreeVIEw1.Selected; 
 TreeVIEw1.Items.Delete(node); 
 
 TButton(Sender).Caption := '用 TTreeNodes 刪除'; 
end; 
 
{插入, 只能通過 TTreeNodes} 
procedure TForm1.Button5Click(Sender: TObject); 
var 
 node: TTreeNode; 
begin 
 if not Boolean(TreeVIEw1.Items) then Exit; 
 if TreeVIEw1.SelectionCount = 0 then Exit; 
 node := TreeVIEw1.Selected; 
 TreeVIEw1.Items.Insert(node, '新成員'); 
 
 TButton(Sender).Caption := '插入'; 
end; 
 
end. 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved