I have VirtualStringTree
with some items and I want to implement a feature similar to what a ComboBox
has.
It works like this: when 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 start with it. But if I start typing a word it should select the item corresponding to the one I typed.
Ex: If I press the letter f
once it will select the first item that starts with the letter f
. If I press it three times, you should select the third item that starts with the letter f
.
But if I quickly type for
it should select the item that starts with for
, for example fornecedor
What I have been able to do so far is to select the item if I press a letter, but it does not go through all the items as I need them and it does not work if I type the excerpt of a word.
Follow the search code:
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 in KeyPress
of 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;