Get next/previous item in an ObjectListView
Asked Answered
B

1

7

My application uses the Q and A keys to move to the next item in an ObjectListView, which works great as long as the user doesn't sort the list items using one of the column headers. If the user has sorted, then pressing the Q/A keys causes the list to jump around the list items, as it seems to use the original order, rather than the current order.

So I am looking for a solution that allows the user to move to the next item even after the user has sorted. The code I currently have (just for the A key) is below:

if (e.KeyCode == Keys.A)
{
  OLVListItem next = listSession.GetNextItem(listSession.SelectedItem);
  if (next == null)
  {
    return;
  }

  listSession.SelectedObject = (Session)next.RowObject;
  listSession.EnsureModelVisible((Session)next.RowObject);
}
Bartie answered 24/4, 2013 at 9:16 Comment(2)
i got the same problem here.. and also, how to GetNextItem when the items were grouped..Immiscible
There are some Methods that refer to item indexing depending on display order like GetNthItemInDisplayOrder() and GetDisplayOrderOfItemIndex(), but i didn't manage to come up with a working workaround.Megrim
B
4

Right this seems to work e.g. go down the displayed items:

int index = listSession.GetDisplayOrderOfItemIndex(listSession.SelectedItem.Index);
OLVListItem next = listSession.GetNthItemInDisplayOrder(index + 1);

and to go up the displayed items:

int index = listSession.GetDisplayOrderOfItemIndex(listSession.SelectedItem.Index);
OLVListItem next = listSession.GetNthItemInDisplayOrder(index - 1);
Bartie answered 25/4, 2013 at 6:40 Comment(1)
This won't work well for TreeListView because going down could potentially mean jumping out of the current parent. I'd want to get a null or -1 back if there are no more siblings in that direction.Melanoid

© 2022 - 2024 — McMap. All rights reserved.