Search code examples
delphifiremonkeydelphi-10.3-rio

How to bring a TreeViewItem in the visible (scroll) area


I'm looking for a way to navigate by code to an item in a tree view. The object should be moved into the visible area. I could not find a method in either TTreeView or TTreeViewItem.

The following approach does not work under all circumstances because the item position is not always updated:

procedure TfmxMain.MakeItemVisible(Item: TTreeViewItem);
begin
  trvMyTreeView.ViewportPosition :=
    TPointF.Create(min(Item.Position.X - trvSlideGroups.ClientWidth / 2, 0),
     min(Item.Position.Y - trvSlideGroups.ClientHeight / 2, 0));
end;

Solution

  • After deep analysis of FMX.TreeView I found a simple solution:

    procedure TfmxMain.MakeItemVisible(Item: TTreeViewImageItem);
    begin
      Item.Deselect;
      Item.Select;
    end;
    

    This simple manipulation calls the internal method TCustomTreeView.UpdateSelection, that moves the selected item to the visible area.