How do I search a Virtualstringtree in Delphi without using Edit?

Asked

Viewed 152 times

0

I have a VirtualStringTree with some items and I want to implement in it a resource similar to what a ComboBox possesses.

It works like this: when pressing a certain letter, he must select the first item of the grid, as I press the same letter he goes through the other items that start with it. But if I start typing a word it must select the item corresponding to what I typed.

Ex: If I press the letter f once it will select the first item starting with the letter f. If I press it these times you must select the third item that starts with the letter f.

But if I type quickly for he must select the item that starts with for, for example fornecedor

What I’ve been able to do so far is that it selects the item if I press a letter, but it doesn’t go through all the items as I need and it doesn’t even work if I type the snippet 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 on 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;

1 answer

0

As incredible as it may seem VirtualStringTree already owns this resource. Just enable the property IncrementalSearch, in this case I left isAll searching for items starting with a certain letter or an excerpt from the item name.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.