C #: How would you iterate through the items in a list using the next and previous buttons?
C #: How can you cycle through the items in a list using the next and previous buttons?
Background:
Let's say you have 10 items in a list.
There are two buttons next to the list with names and further.
Problem:
How would you switch between these list items after clicking Next and Previous?
If you just want to select the next element (so with elements a, b, c, go from b selected to selected c), you can do this:
if (listView.SelectedIndices.Count > 0)
{
int oldSelection = listView.SelectedIndices[0];
listView.SelectedIndices.Clear();
if (oldSelection + 1 >= listView.Items.Count)
listView.SelectedIndices.Add(0);
else
listView.SelectedIndices.Add(oldSelection + 1);
}
source to share
I think you are saying you want to implement paging?
If so, just keep a record of your current page number in the index, and when you click the next button, increment the page number, extract that page, and write it down to the list.
There is an automatic way to do this if you are implementing data providers etc. Check out the documentation on ListView for how to do this. You will need to give the button a special "action" for it to work.
For more information: http://msdn.microsoft.com/en-us/library/bb515102.aspx
source to share
int oldSelection = ListView1.SelectedIndices[0];
if(*NextOptionSelected* && oldSelection + 1 < ListView1.Items.Count)
{
ListView1.Items[oldSelection + 1].Selected = true;
ListView1.Items[oldSelection + 1].Focused = true;
ListView1.Items[oldSelection].Selected = false;
ListView1.Items[oldSelection].Focused = false;
}
else if(*LastOptionSelected* && oldSelection > 0)
{
ListView1.Items[oldSelection - 1].Selected = true;
ListView1.Items[oldSelection - 1].Focused = true;
ListView1.Items[oldSelection].Selected = false;
ListView1.Items[oldSelection].Focused = false;
}
ListView1.EnsureVisible(ListView1.SelectedIndices[0]);
source to share
//UpOrDown is a +1 or -1
void Page(int UpOrDown){
//Determine if something is selected
if (listView.SelectedIndices.Count > 0)
{
int oldIndex = listView.SelectedIndices(0);
listView.SelectedIndices.Clear();
//Use mod!
int numberOfItems = listView.Items.Count();
listView.SelectedIndices.Add((oldIndex + UpOrDown) % numberOfItems)
}
}
Using modulus
division, I feel like people have forgotten about this guy.
source to share
put +1 for next and -1 for prev data here is the code: -
Dim CurrentRow As Integer CurrentRow = Form2.ListView1.Items.IndexOf(Form2.ListView1.FocusedItem) CurrentRow =CurrentRow + 1 Form2.ListView1.Items(CurrentRow).Selected = True Form2.ListView1.Items(CurrentRow).Focused = True
This is the vb code to go to the next line in the list 1. Add a listview control to the form 2. Add one button to form 3. Double click this button and right above the code above.
source to share
tested by aproach using page method, null / out of index range-proof:
private void Page(int UpOrDown, ListView list)
{
//Determine if something is selected
list.Focus();
if (list.SelectedIndices.Count == 0)
{
if (list.Items.Count > 0)
{
list.Items[0].Selected = true;
list.SelectedIndices.Add(0);
list.Items[0].Focused = true;
list.EnsureVisible(list.Items[0].Index);
list.TopItem = list.Items[0];
}
}
if (list.SelectedIndices.Count > 0)
{
if (list.SelectedIndices[0] == null)
{
list.SelectedIndices.Add(0);
}
int oldIndex = list.SelectedIndices[0];
list.SelectedIndices.Clear();
//Use mod!
int numberOfItems = list.Items.Count;
if (oldIndex + UpOrDown >= 0 && oldIndex + UpOrDown <= list.Items.Count-1)
{
list.SelectedIndices.Add((oldIndex + UpOrDown)%numberOfItems);
list.SelectedItems[0].Selected = true;
list.SelectedItems[0].Focused = true;
list.EnsureVisible(list.SelectedItems[0].Index);
//list.TopItem = list.SelectedItems[0];
}
}
}
source to share