Search code examples
c#winformslistboxlistboxitem

how back the the first index after scroling to the last index on listbox


I use this code to move to next index listBox1.SelectedIndex = listBox1.SelectedIndex +1; but the problem is when I am on the last index and I click the button next that have that code is crash any suggestions to make next button back to the first index after click it ??


Solution

  • Code to select next item:

    if (listBox1.SelectedIndex < listBox1.Items.Count - 1)
    {
        listBox1.SelectedIndex += 1;
    }
    else
    {
        listBox1.SelectedIndex = 0;
    }