Delphi 如何使用TVirtualStringTree进行增量搜索
我正在学习Delphi 如何使用TVirtualStringTree进行增量搜索,delphi,virtualtreeview,tvirtualstringtree,Delphi,Virtualtreeview,Tvirtualstringtree,我正在学习TVirtualStringTree用法,必须实现增量搜索。当用户在TEdit中输入字符时,我想将焦点节点移动到树中的第一个符合条件的节点 我正在阅读我能找到的所有演示和示例代码,但似乎找不到这方面的起点。有人能让我从伪代码或更好的代码开始吗?该控件已经支持增量搜索。您不需要添加任何编辑控件;只要开始在树控件中输入,它就会选择下一个匹配的节点。根据需要设置IncrementalSearch、IncrementalSearchDirection、IncrementalSearchStar
TVirtualStringTree
用法,必须实现增量搜索。当用户在TEdit
中输入字符时,我想将焦点节点移动到树中的第一个符合条件的节点
我正在阅读我能找到的所有演示和示例代码,但似乎找不到这方面的起点。有人能让我从伪代码或更好的代码开始吗?该控件已经支持增量搜索。您不需要添加任何编辑控件;只要开始在树控件中输入,它就会选择下一个匹配的节点。根据需要设置
IncrementalSearch
、IncrementalSearchDirection
、IncrementalSearchStart
和IncrementalSearchTimeout
属性
要选择与给定条件匹配的第一个节点,请使用
IterateSubtree
。编写一个匹配TVTGetNodeProc
签名的方法,根据搜索条件检查单个节点。它将为树中的每个节点调用,如果节点匹配,则应将Abort
参数设置为true。使用IterateSubtree
(名为Data
)的第三个参数将搜索项与任何其他搜索条件一起传递给回调函数。我已经删除了一些不必要的代码,但现在您可以:
unit fMyForm;
interface
uses
Windows, Messages, Forms, StdCtrls, VirtualTrees, StrUtils;
type
TfrmMyForm = class(TForm)
vstMyTree: TVirtualstringTree;
myEdit: TEdit;
procedure myEditChange(Sender: TObject);
private
procedure SearchForText(Sender: TBaseVirtualTree; Node: PVirtualNode; Data: Pointer; var Abort: Boolean);
end;
PDatastructure = ^TDatastructure;
TDatastructure = record
YourFieldHere : Widestring;
end;
implementation
{$R *.dfm}
procedure TfrmMyForm.SearchForText(Sender: TBaseVirtualTree; Node: PVirtualNode; Data: Pointer; var Abort: Boolean);
var
NodeData: PDatastructure; //replace by your record structure
begin
NodeData := Sender.GetNodeData(Node);
Abort := AnsiStartsStr(string(data), NodeData.YourFieldHere); //abort the search if a node with the text is found.
end;
procedure TfrmMyForm.myEditChange(Sender: TObject);
var
foundNode : PVirtualNode;
begin
inherited;
//first param is your starting point. nil starts at top of tree. if you want to implement findnext
//functionality you will need to supply the previous found node to continue from that point.
//be sure to set the IncrementalSearchTimeout to allow users to type a few characters before starting a search.
foundNode := vstMyTree.IterateSubtree(nil, SearchForText, pointer(myEdit.text));
if Assigned (foundNode) then
begin
vstMyTree.FocusedNode := foundNode;
vstMyTree.Selected[foundNode] := True;
end;
end;
end.
你的问题令人困惑,当你说
而不是必须实现增量搜索时
你的意思是你必须使用增量搜索还是不使用增量搜索?@PeterVonča“not”应该是“I”。