BrightIdeasSoftware.ObjectListView.EnsureGroupVisible C# (CSharp) Method

EnsureGroupVisible() public method

Scroll the listview so that the given group is at the top.

If the group is already visible, the list will still be scrolled to move the group to the top, if that is possible.

This only works when the list is showing groups (obviously).

This does not work on virtual lists, since virtual lists don't use ListViewGroups for grouping. Use VirtualObjectListView.EnsureNthGroupVisible instead.

public EnsureGroupVisible ( ListViewGroup lvg ) : void
lvg ListViewGroup The group to be revealed
return void
        public virtual void EnsureGroupVisible(ListViewGroup lvg)
        {
            if (!this.ShowGroups || lvg == null)
                return;

            int groupIndex = this.Groups.IndexOf(lvg);
            if (groupIndex <= 0) {
                // There is no easy way to scroll back to the beginning of the list
                int delta = 0 - NativeMethods.GetScrollPosition(this, false);
                NativeMethods.Scroll(this, 0, delta);
            } else {
                // Find the display rectangle of the last item in the previous group
                ListViewGroup previousGroup = this.Groups[groupIndex - 1];
                ListViewItem lastItemInGroup = previousGroup.Items[previousGroup.Items.Count - 1];
                Rectangle r = this.GetItemRect(lastItemInGroup.Index);

                // Scroll so that the last item of the previous group is just out of sight,
                // which will make the desired group header visible.
                int delta = r.Y + r.Height / 2;
                NativeMethods.Scroll(this, 0, delta);
            }
        }
ObjectListView