如何在TreeView中检索内容(50分)

  • 主题发起人 主题发起人 holyszq
  • 开始时间 开始时间
H

holyszq

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在TreeView中检索到指定的内容,并把焦点指向改内容。
 
遍历,使用递归
 
function searchstr(treenode:ttreenodes):integer;
var i,val:integer;
begin
for i:=0 to treeview1.items.count-1 do
begin
if treeview1.items.item=s then
val:=i;
end;
result:=val;
end;
button1.click事件中
begin
treeview1.Items.Item[searchstr(....)].Selected;
treeview1.Items.Item[searchstr(....)].].Expand(true);
end;
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
TreeView1: TTreeView;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure ReadTreeViewData(tnNode:TTreeNode;strCondition:AnsiString);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ReadTreeViewData(tnNode:TTreeNode;strCondition:AnsiString);
var
tnCurrent:TTreeNode;
i:integer;
begin
//其中tnNode为树的节点,strCondition为查找的内容
if(tnNode.Text=strCondition) then
tnNode.Selected:=true;
if(tnNode.Count<>0) then
begin
for i:=0 to tnNode.Count-1 do
ReadTreeViewData(tnNode.Item,strCondition);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
TreeView1.MultiSelect:=true;
ReadTreeViewData(TreeView1.Items.GetFirstNode,'1');
end;

end.
 
用这个函数获得节点
function TTreeFrame.GetNodeFromLabel(pLabel: string): TTreeNode;
var i:integer;
begin
Result:=nil;
for i:=0 to TreeView.Items.Count - 1 do
begin
if TreeView.Items.Text = pLabel then
begin
Result:=TreeView.Items;
break;
end;
end;
end;
然后再选定该节点
GetNodeFromLabel(...).Selected:=true ;


 
接受答案了.
 
后退
顶部