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

TTreeView講解 [5] - TTreeNode.Level 屬性

編輯:Delphi

 本例效果圖:

TTreeView講解 [5] - TTreeNode.Level 屬性

unit Unit1; 
 
interface 
 
uses 
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
 Dialogs, ComCtrls, StdCtrls, Grids, ExtCtrls; 
 
type 
 TForm1 = class(TForm) 
  TreeView1: TTreeVIEw; 
  Memo1: TMemo; 
  Panel1: TPanel; 
  Button1: TButton; 
  Button2: TButton; 
  Button3: TButton; 
  procedure FormCreate(Sender: TObject); 
  procedure Button1Click(Sender: TObject); 
  procedure Button2Click(Sender: TObject); 
  procedure Button3Click(Sender: TObject); 
 end; 
 
var 
 Form1: TForm1; 
 
implementation 
 
{$R *.dfm} 
 
{初始化 TreeVIEw1} 
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); 
var 
 i: Integer; 
begin 
 Memo1.Clear; 
 for i := 0 to TreeVIEw1.Items.Count - 1 do 
  Memo1.Lines.Add(TreeVIEw1.Items[i].Text); 
end; 
 
{使用 Level 屬性和 Tab 字符格式化列表} 
procedure TForm1.Button2Click(Sender: TObject); 
var 
 i: Integer; 
 s: string; 
begin 
 Memo1.Clear; 
 for i := 0 to TreeVIEw1.Items.Count - 1 do 
 begin 
  s := StringOfChar(#9, TreeVIEw1.Items[i].Level); 
  s := s + TreeVIEw1.Items[i].Text; 
  Memo1.Lines.Add(s); 
 end; 
end; 
 
{上面那個不好看, 這個是用了 4 個空格做 Level 標識} 
procedure TForm1.Button3Click(Sender: TObject); 
var 
 i: Integer; 
 s: string; 
begin 
 Memo1.Clear; 
 for i := 0 to TreeVIEw1.Items.Count - 1 do 
 begin 
  s := StringOfChar(#32, TreeVIEw1.Items[i].Level * 4); 
  s := s + TreeVIEw1.Items[i].Text; 
  Memo1.Lines.Add(s); 
 end; 
end; 
 
end. 

 之前忘了 StringOfChar 函數的存在, 曾經把程序寫作:

unit Unit1; 
 
interface 
 
uses 
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
 Dialogs, ComCtrls, StdCtrls, Grids, ExtCtrls; 
 
type 
 TForm1 = class(TForm) 
  TreeView1: TTreeVIEw; 
  Memo1: TMemo; 
  Panel1: TPanel; 
  Button1: TButton; 
  Button2: TButton; 
  Button3: TButton; 
  procedure FormCreate(Sender: TObject); 
  procedure Button1Click(Sender: TObject); 
  procedure Button2Click(Sender: TObject); 
  procedure Button3Click(Sender: TObject); 
 end; 
 
var 
 Form1: TForm1; 
 
implementation 
 
{$R *.dfm} 
 
{初始化 TreeVIEw1} 
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); 
var 
 i: Integer; 
begin 
 Memo1.Clear; 
 for i := 0 to TreeVIEw1.Items.Count - 1 do 
  Memo1.Lines.Add(TreeVIEw1.Items[i].Text); 
end; 
 
{使用 Level 屬性和 Tab 字符格式化列表} 
procedure TForm1.Button2Click(Sender: TObject); 
var 
 arr: array[0..MAXBYTE] of Char; 
 i: Integer; 
 s: string; 
begin 
 Memo1.Clear; 
 FillChar(arr, Length(arr), #9); 
 for i := 0 to TreeVIEw1.Items.Count - 1 do 
 begin 
  s := Copy(arr, 0, TreeVIEw1.Items[i].Level); 
  s := s + TreeVIEw1.Items[i].Text; 
  Memo1.Lines.Add(s); 
 end; 
end; 
 
{上面那個不好看, 這個是用了 4 個空格做 Level 標識} 
procedure TForm1.Button3Click(Sender: TObject); 
var 
 arr: array[0..MAXBYTE] of Char; 
 i: Integer; 
 s: string; 
begin 
 Memo1.Clear; 
 FillChar(arr, Length(arr), #32); 
 for i := 0 to TreeVIEw1.Items.Count - 1 do 
 begin 
  s := Copy(arr, 0, TreeVIEw1.Items[i].Level * 4); 
  s := s + TreeVIEw1.Items[i].Text; 
  Memo1.Lines.Add(s); 
 end; 
end; 
 
end. 



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