Search code examples
androiddelphiscrolloffsetfiremonkey

How get scroll offset of each component that has a scroll bar?


Some people ask me: How to get scroll offset of each component that has a scroll bar and how to get coordinates of some items (maybe TListBoxitem in TListBox) on visible part of component - example for showing TPopup for current item?

Popup.Position.X:= ListBox.ItemByIndex(0).Position.X;
Popup.Position.Y:= ListBox.ItemByIndex(0).Position.Y;

This code show TPopup on the top-left corner of Item(0) and is visible.

When is visible Item(100) TPopup is not on the top-left corner and is not visible:

Popup.Position.X:= ListBox.ItemByIndex(100).Position.X;
Popup.Position.Y:= ListBox.ItemByIndex(100).Position.Y;

How make TPopup visible on the top-left corner for each item?


Solution

  • The answer is very simply (example for TListBox):

    var
      offsetX, offsetY: single;
    begin
      offsetX:= myListBox.ViewportPosition.X;
      offsetY:= myListBox.ViewportPosition.Y;
    end;
    

    And how make visible TPopup on top-left corner for each items:

    Popup.Position.X:= ListBox.ItemById(0).Position.X - ListBox.ViewportPosition.X;
    Popup.Position.Y:= listBox.ItemById(0).Position.Y - ListBox.ViewportPosition.Y;
    .
    .
    .
    Popup.Position.X:= ListBox.ItemById(100).Position.X - ListBox.ViewportPosition.X;
    Popup.Position.Y:= listBox.ItemById(100).Position.Y - ListBox.ViewportPosition.Y;