【问题标题】:How to incremental search using TVirtualStringTree如何使用 TVirtualStringTree 进行增量搜索
【发布时间】:2013-06-06 11:53:11
【问题描述】:

我正在学习TVirtualStringTree 的用法,并且必须实现增量搜索。当用户在TEdit 中输入字符时,我想将焦点节点移动到树中的第一个合格节点。

我正在阅读我能找到的所有演示和示例代码,但似乎无法找到一个起点。任何人都可以让我开始使用伪代码或更好的代码吗?

【问题讨论】:

  • 你的问题很混乱,当你说and not must implement an incremental search时你的意思是你必须使用增量搜索吗?
  • @PeterVonča "not" 应该是 "I"。

标签: delphi virtualtreeview tvirtualstringtree


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-08
      • 1970-01-01
      • 2015-07-08
      • 2018-07-26
      • 1970-01-01
      • 2015-01-03
      • 2016-05-31
      相关资源
      最近更新 更多