2013-06-10 3 views
3

TVirtualStringTree 사용법을 배우고 있으며 증분 검색을 구현해야합니다. 사용자가 문자를 TEdit에 입력하면 집중 노드를 트리의 첫 번째 노드로 이동하려고합니다.TVirtualStringTree를 사용하여 증분 검색하는 방법

나는 모든 데모와 예제 코드를 통해 읽고 있는데, 이것에 대한 시작 장소를 찾을 수없는 것 같습니다. 누구든지 나를 의사 코드 이상으로 시작할 수 있습니까?

+0

당신이 말하는 '및 증분 search'를 구현해야하지 않을 경우 귀하의 질문은, 혼란 당신이 사용할 필요가 있음을 의미합니까 증분 검색 여부 – Peter

+0

@ PeterVonča "not"는 "I"이어야합니다. – TedOnTheNet

답변

4

컨트롤은 증분 검색을 이미 지원합니다. 편집 컨트롤을 추가 할 필요는 없습니다. 트리 컨트롤에 입력하기 만하면 다음 일치 노드가 선택됩니다. 필요에 따라 IncrementalSearch, IncrementalSearchDirection, IncrementalSearchStartIncrementalSearchTimeout 속성을 설정하십시오.

주어진 조건과 일치하는 첫 번째 노드를 선택하려면 IterateSubtree을 사용하십시오. TVTGetNodeProc의 서명과 일치하는 메소드를 작성하여 검색 기준에 대해 단일 노드를 점검하십시오. 트리의 각 노드에 대해 호출되고 노드가 일치하면 Abort 매개 변수를 true로 설정해야합니다. IterateSubtree (Data)의 세 번째 매개 변수를 사용하여 다른 검색 조건과 함께 콜백 함수에 검색어를 전달하십시오.

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. 
관련 문제