本例效果图:
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.