How to search a VirtualStringTree in Delphi without using Edit?
I have a VirtualStringTree
with some items and want to implement in it a feature similar to what a ComboBox
has.
Works like this: by pressing a certain letter it should select the first item of the grid, as I press the same letter it goes through the other items that begin with it. But if I start typing a word it should select the item corresponding to what I typed.
Ex: if I press the letter f
a instead it will select the first item that starts with the letter f
. If I press it three times you must select the third item that begins with the letter f
.
but if I quickly type for
it should select the item that starts with for
, for example fornecedor
What I've been able to do so far is for it to select the item in case I press a letter, but it doesn't go through all the items as I need and doesn't even work in case I type the snippet of a word.
Follows the code that does the search:
procedure TfrmGrid.SearchForText(Sender: TBaseVirtualTree; Node: PVirtualNode;
Data: Pointer; var Abort: Boolean);
var
NodeData: PPonteiro;
begin
NodeData := PPonteiro(vtvGrid.GetNodeData(Node)^);
// Interrompe a pesquisa caso encontre um nĂ³ com o texto correspondente
Abort := AnsiStartsText(string(data), NodeData.ITEM);
if vtvGrid.GetFirstSelected = Node then
Abort := False;
end;
What goes into KeyPress
from VirtualStringTree
:
procedure TfrmGrid.vtvGridKeyPress(Sender: TObject; var Key: Char);
var
foundNode : PVirtualNode;
begin
inherited;
foundNode := vtvGrid.IterateSubtree(nil, SearchForText, Pointer(Key));
if Assigned (foundNode) then
begin
vtvGrid.FocusedNode := foundNode;
vtvGrid.Selected[foundNode] := True;
end;
end;
1 answers
Oddly enough VirtualStringTree
already has this feature. Just enable the property IncrementalSearch
, in this case I left isAll
that searches for items started with a certain letter or by excerpt of the name of the item.