源码:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls; 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); end; var Form1: TForm1; implementation {$R *.dfm} var i: Integer; {这四个按钮的功能是一模一样的} procedure TForm1.Button1Click(Sender: TObject); begin Self.Text := TreeView1.Items.Item[i].Text; if i < TreeView1.Items.Count-1 then Inc(i) else i := 0; TreeView1.SetFocus; TreeView1.Select(TreeView1.Items.Item[i]); end; {TreeView1.Items.Item[i] 是默认属性, 可以简写为: TreeView1.Items[i]} procedure TForm1.Button2Click(Sender: TObject); begin Text := TreeView1.Items[i].Text; if i < TreeView1.Items.Count-1 then Inc(i) else i := 0; TreeView1.SetFocus; TreeView1.Select(TreeView1.Items[i]); end; {每个元素都是 TTreeNode 类型的} procedure TForm1.Button3Click(Sender: TObject); var node: TTreeNode; begin node := TreeView1.Items[i]; Text := node.Text; if i < TreeView1.Items.Count-1 then Inc(i) else i := 0; TreeView1.SetFocus; TreeView1.Select(node); end; {尽量使用 TTreeNodes 与 TTreeNode} procedure TForm1.Button4Click(Sender: TObject); var Nodes: TTreeNodes; node: TTreeNode; begin Nodes := TreeView1.Items; node := Nodes[i]; Text := node.Text; if i < Nodes.Count-1 then Inc(i) else i := 0; TreeView1.SetFocus; TreeView1.Select(node); end; end.