本文整理自网络,侵删。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, Grids;
type
TForm1 = class(TForm)
tv1: TTreeView;
Button1: TButton;
Button2: TButton;
tv2: TTreeView;
Button3: TButton;
Button4: TButton;
StringGrid1: TStringGrid;
mmo1: TMemo;
Button5: TButton;
Button6: TButton;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure tv1Change(Sender: TObject; Node: TTreeNode);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{节点转移}
var
IsNode1 : Boolean = True;{全局变量,判断节点在哪个控件里面}
procedure TForm1.Button1Click(Sender: TObject);
begin
if IsNode1 then //判断是否转移过
begin
if Boolean(tv1.Items.Count) then //节点不为空
begin
tv2.Items := tv1.Items;
tv1.Items.Clear;
IsNode1 := False; //是否转移的标志物
end;
end else //二次转移
if Boolean(tv2.Items.Count) then //节点不为空
begin
tv1.Items := tv2.Items;
tv2.Items.Clear;
IsNode1 := True;
end;
end;
{添加节点}
procedure TForm1.Button2Click(Sender: TObject);
var
CurrNode: TTreeNodes;//节点数组
TemNode: TTreeNode; //临时节点
begin
CurrNode := tv1.Items;//关联树形控件1
{添加一级节点}
TemNode := CurrNode.Add(nil, '一级001');//add增加兄弟节点
CurrNode.Add(TemNode, '一级002');
CurrNode.Add(TemNode, '一级003');
{添加二级节点}
TemNode := CurrNode.AddChild(TemNode, '二级001');//AddChild增加子节点
CurrNode.Add(TemNode, '二级002');
CurrNode.Add(TemNode, '二级003');
{添加三级节点}
TemNode := CurrNode.AddChild(TemNode, '三级001');
CurrNode.Add(TemNode, '三级002');
CurrNode.Add(TemNode, '三级003');
{添加四级节点}
TemNode := CurrNode.AddChild(TemNode, '四级001');
CurrNode.Add(TemNode, '四级002');
CurrNode.Add(TemNode, '四级003');
CurrNode.AddFirst(TemNode, '四级No1'); //AddFirst增加兄弟节点,排兄弟最前
end;
{遍历读取每一项}
var
I : Integer = 0;
procedure TForm1.Button3Click(Sender: TObject);
begin
Self.Button3.Caption := tv1.Items.Item[i].Text; //按钮输出
if i < tv1.Items.Count-1 then Inc(i) else i := 0;{tv1.Items.Count为总项数}
tv1.SetFocus; //获取焦点
tv1.Select(tv1.Items.Item[i]);//选中该项
{读取每一项方法2:
procedure TForm1.Button3Click(Sender: TObject);
var
CurrNode : TTreeNodes; //节点数组
TemNode : TTreeNode;//单个节点
begin
CurrNode := tv1.Itemsl//节点
TemNode := CurrNode[I];
Self.Button3.Caption := tv1.Items.Item[i].Text; //按钮输出
if i < tv1.Items.Count-1 then Inc(i) else i := 0;//tv1.Items.Count为总项数
tv1.SetFocus; //获取焦点
tv1.Select(TemNode);//选中该项
end;}
end;
{清空TTreeList}
procedure TForm1.Button4Click(Sender: TObject);
begin
tv1.Items.Clear;
tv2.Items.Clear;
end;
{读取数据}
procedure TForm1.Button5Click(Sender: TObject);
var
i: Integer;
begin
mmo1.Clear;
for i := 0 to tv1.Items.Count - 1 do//遍历每一项
mmo1.Lines.Add(tv1.Items[i].Text);//插入
end;
{按级别读取数据}
procedure TForm1.Button6Click(Sender: TObject);
var
i: Integer;
s: string;
begin
mmo1.Clear;
for i := 0 to tv1.Items.Count - 1 do //遍历
begin
s := StringOfChar(#32, tv1.Items[i].Level*4);//读取级别,多少级就多少个
s := s + tv1.Items[i].Text;
mmo1.Lines.Add(s);
end;
end;
{初始化}
procedure TForm1.FormCreate(Sender: TObject);
begin
{初始化 StringGrid1}
with StringGrid1 do begin
//Align := alClient;
//FixedRows := 0;//固定住行避免被改变
ColCount := 2;//列数
ColWidths[0] := 80;//第一列宽度
ColWidths[1] := 60;//第二列宽度
DefaultRowHeight := 18; //每个框的高度
RowCount := 15; //有15行
Cells[0,0] := '当前选择';
Cells[0,1] := '序号';
Cells[0,2] := '所在级别';
Cells[0,3] := '在兄弟中排行';
Cells[0,4] := '下级总数';
Cells[0,5] := '上级元素';
Cells[0,6] := '上一个';
Cells[0,7] := '下一个';
Cells[0,8] := '上一个兄弟';
Cells[0,9] := '下一个兄弟';
Cells[0,10] := '上一个可见';
Cells[0,11] := '下一个可见';
Cells[0,12] := '第一个下级';
Cells[0,13] := '最后一个下级';
Cells[0,14] := '总数';
end;
end;
{读取每个节点的内容}
procedure TForm1.tv1Change(Sender: TObject; Node: TTreeNode);
begin
with StringGrid1 do begin
{清除第二列的数据}
StringGrid1.Cols[1].Clear;
{当前选择}
Cells[1,0] := Node.Text;
{序号; AbsoluteIndex 是绝对序号}
Cells[1,1] := IntToStr(Node.AbsoluteIndex);
{所在级别}
Cells[1,2] := IntToStr(Node.Level);
{在兄弟中排行}
Cells[1,3] := IntToStr(Node.Index);
{下级总数}
Cells[1,4] := IntToStr(Node.Count);
{上级元素}
if Boolean(Node.Parent) then Cells[1,5] := Node.Parent.Text;
{上一个}
if Boolean(Node.GetPrev) then Cells[1,6] := Node.GetPrev.Text;
{下一个}
if Boolean(Node.GetNext) then Cells[1,7] := Node.GetNext.Text;
{上一个兄弟}
if Boolean(Node.getPrevSibling) then Cells[1,8] := Node.getPrevSibling.Text;
{下一个兄弟}
if Boolean(Node.getNextSibling) then Cells[1,9] := Node.getNextSibling.Text;
{上一个可见}
if Boolean(Node.GetPrevVisible) then Cells[1,10] := Node.GetPrevVisible.Text;
{下一个可见}
if Boolean(Node.GetNextVisible) then Cells[1,11] := Node.GetNextVisible.Text;
{第一个下级}
if Node.HasChildren then Cells[1,12] := Node.getFirstChild.Text;
{最后一个下级}
if Node.HasChildren then Cells[1,13] := Node.GetLastChild.Text;
{总数}
Cells[1,14] := IntToStr(Node.Owner.Count);
end;
end;
end.
相关阅读 >>
Delphi winapi: setvolumelabel - 设置磁盘卷标
更多相关阅读请进入《Delphi》频道 >>