查找搜索TreeView节点

字体大小: 中小 标准 ->行高大小: 标准
从当前节点的子节点开始查找,并不仅查找子节点,也会遍历同级和父级节点
procedure TFrmWindowView.btnSearchClick(Sender: TObject);
//查找节点内容

 function FindChild(
     const ANode   : TTreeNode;
     const AText   : string;
     const AStart  : Integer = 0
     ):Boolean;
 //遍历子节点
 var
   i : Integer;
 begin
   Result  :=  False;
   for i := AStart to ANode.Count - 1 do
   begin
     if Pos(AText,UpperCase(ANode.Item[i].Text)) > 0 then
     begin
       ANode.Item[i].Selected  :=  True;
       Result  :=  True;
       Exit;
     end;
     Result  :=  FindChild(ANode.Item[i],AText);
     if Result then Exit;
   end;
 end;

 function FindParent(
     const ANode   : TTreeNode;
     const AText   : string
     ):Boolean;
 //遍历父节点
 begin
   Result  :=  False;
   if not Assigned(ANode.Parent) then Exit;
   Result  :=  FindChild(ANode.Parent,AText,ANode.Index + 1);
   if not Result then
     Result  :=  FindParent(ANode.Parent,AText);
 end;

var
 sSearchText : string;
begin
 if not Assigned(tv.Selected) or (edtSearch.Text = '') then Exit;
 sSearchText :=  UpperCase(edtSearch.Text);
 if not FindChild(tv.Selected, sSearchText) then
   FindParent(tv.Selected, sSearchText);
end; 

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/68088.html