Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the listview to show a specific item in the center?

Is there a universal method of placing a specific item (e.g. 500th from 1000) of the listview right in its center? Now I am using this code:

lvData.Items[iIndex].MakeVisible(False);

It's simple but has one flaw - mostly the required item appears at the top or at the bottom of the listview. Yes I know, it's not a big deal to scroll it manually but the way I use it (selecting a point on the graph and viewing the values of the nearby points in the listview) makes this behavior a little uncomfortable.

like image 695
Molochnik Avatar asked Dec 05 '22 22:12

Molochnik


2 Answers

You can use DisplayRect of an item to determine where it currently lives. Given ListView1 is the listview, li is the list item and R is a TRect variable

R := li.DisplayRect(drBounds);
ListView1.Scroll(0, R.Top - ListView1.ClientHeight div 2);

will scroll the item in the center, provided there are enough items.

like image 138
Sertac Akyuz Avatar answered May 23 '23 09:05

Sertac Akyuz


Just to give an idea. The TopItem gives the topmost item in view and VisibleRowCount gives how many visible rows there are. To make this complete, make a sanity check for the new index.

if (lvData.TopItem < iIndex) then
  adjustedIndex := iIndex-(lvData.VisibleRowCount div 2)   
else
  adjustedIndex := iIndex+(lvData.VisibleRowCount div 2);
// Check adjustedIndex
if (adjustedIndex < 0) then
  adjustedIndex := 0;
if (adjustedIndex >= lvData.Items.Count) then
  adjustedIndex := lvData.Items.Count-1;

lvData.Items[adjustedIndex].MakeVisible(false);
like image 36
LU RD Avatar answered May 23 '23 11:05

LU RD