0
votes

I need to be able to get and set the top item in a listbox - something like a ListBox.TopItem property would be great. I have been unable to find anything that does this job. Any ideas would be greatly appreciated please.


Edit:

For example: Listbox1 and ListBox2 items are the following:

1 Data1 
2 Data2 
3 More Data 
4 More again 
5 Yet more 
6 and this will do.

Showing in 2 listboxes, both 3 items high:

1 Data1
2 Data2
3 More Data

and I want to programmatically make them show

3 More Data 
4 More again 
5 Yet more

and I want to find out what item the top one is.

1
Have you looked in the documentation for FMX.ListBox.TListBox.Items? Use Items to get or set the list of items in the list as strings. Or as the docs continue if you want to access an item as TListBoxItem: Use the ListItems property to access an item by specifying its index. This property returns the item as a TListBoxItem object. Index 0 refers to the topmost item.Tom Brunberg
FMX.ListBox.tListBox.Items is a tStrings object, with no Index associated. tListBox.Index has something to do with child objects, and changing it does nothing to adjust the top item displayed in the ListBox. I am trying to adjust which item can be seen as the top item displayed in the ListBox, or to find out which is the top item displayed. There is nothing under Items nor Index which I can find to do this.erik wilson

1 Answers

0
votes

(previous answer text deleted)

Update after clarification of question.

Ok, so you want

a) to scroll the list programmatically

b) to read the topmost item programmatically

The following scrolls the list so that a given item becomes the top visible item. Note however, that the list can not be scrolled up beyond the point that there would be empty space between the last item and the viewport's bottom.

procedure TForm25.SetTopVisibleItem(idx: integer);
var
  ptf: TPointF;
begin
  ptf := PointF(0, idx * ListBox1.ItemHeight);
  ListBox1.ViewportPosition := ptf;
end;

To get the current topmost item in the viewport:

function TForm25.GetTopVisibleItem: TListBoxItem;
var
  x, y: single;
begin
  x := 3; // a small offset is required to assure that
  y := 3; // the point is within an item
  result := ListBox1.ItemByPoint(x, y);
end;