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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With