3

I am learning TVirtualStringTree usage and must implement an incremental search. When the user enters characters into a TEdit I want to move the focused node to the first qualifying node in the tree.

I'm reading through all the demo and example code I can find and cannot seem to find a starting place for this. Can anyone get me started with pseudo code or better?

4

2 回答 2

6

该控件已经支持增量搜索。您不需要添加任何编辑控件;只需开始输入树控件,它将选择下一个匹配节点。根据需要设置IncrementalSearchIncrementalSearchDirectionIncrementalSearchStartIncrementalSearchTimeout属性。

要选择与给定条件匹配的第一个节点,请使用IterateSubtree. 编写一个与 的签名匹配的方法,TVTGetNodeProc以根据您的搜索条件检查单个节点。它将为树中的每个节点调用,如果节点匹配,则应将Abort参数设置为 true。使用IterateSubtree(named Data) 的第三个参数将搜索词与任何其他搜索条件一起传递给您的回调函数。

于 2013-06-10T20:06:46.447 回答
4

我已经删除了一些不必要的代码,但你去吧:

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.
于 2013-06-11T09:48:01.927 回答