Link to home
Start Free TrialLog in
Avatar of Todd MacPherson
Todd MacPhersonFlag for Canada

asked on

Listview Items and scrolling issues when promoting and demoting items.

Hi

I have a problem I do not know how to solve. I have a couple of buttons where a user selecets items in a listview and clicks a button causing item to either promote or demote in the listview by one position per click. If the amount of items exceeds the listview height then a  vertical scrollbar is present, and herein lies the problem.

If the user, for example, promotes an item near the bottom of the listview, and it moves up far enough the user can no longer see it unless they manually scroll to it. Is there anyway to make sure that the selected item always stays in view no matter how many items are in the listview?

Thanks
PBLack

PS I have a similar question concerning scrolling issues at  https://www.experts-exchange.com/questions/22479629/See-bottom-item-of-listview-automatically-as-new-items-are-added.html

===============================================
PROMOTE CODE:

        If lstMenu.SelectedIndices.Count = 0 Then
            Beep()
            Exit Sub
        End If

        Dim lvi As ListViewItem = lstMenu.Items(lstMenu.SelectedIndices(0))
        Dim inx As Integer
        Dim temp As Integer
        temp = lstMenu.Items.IndexOf(lvi)
        If temp = 0 Then
            Beep()
            Exit Sub
        End If
        inx = temp - 1

        Dim x As Integer = (lstMenu.SelectedIndices.Count - 1)
        While x >= 0
            lstMenu.Items.RemoveAt(lstMenu.SelectedIndices(x))
            System.Math.Max(System.Threading.Interlocked.Decrement(x), x + 1)
        End While
        Beep()
        lstMenu.Items.Insert(inx, lvi)
=========================================

DEMOTE CODE:

        If lstMenu.SelectedIndices.Count = 0 Then
            Beep()
            Exit Sub
        End If

        Dim lvi As ListViewItem = lstMenu.Items(lstMenu.SelectedIndices(0))
        Dim inx As Integer
        Dim temp As Integer
        Dim Y As Integer
        temp = lstMenu.Items.IndexOf(lvi)
        Y = lstMenu.Items.Count
        If temp = Y - 1 Then
            Beep()
            Exit Sub
        End If
        inx = temp + 1

        Dim x As Integer = (lstMenu.SelectedIndices.Count - 1)
        While x >= 0
            lstMenu.Items.RemoveAt(lstMenu.SelectedIndices(x))
            System.Math.Max(System.Threading.Interlocked.Decrement(x), x + 1)
        End While
        Beep()
        lstMenu.Items.Insert(inx, lvi)

Thanks

PBLack
- PS I have another question of a similar nature involving adding items to a listview
ASKER CERTIFIED SOLUTION
Avatar of Sancler
Sancler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Todd MacPherson

ASKER

added lstMenu.Items(inx).EnsureVisible() to the bottom of each routine

Thanks