ok, on a button click how could i have the listview selected item jump to the next. so if there was a list view with 3 items and the second was selected. On a button clickhow could i make it jump to the third?

-T

Dani AI

Generated

Building on ’s working idea (which solved ’s immediate need), here’s a slightly more robust, scroll-aware approach that handles the common edge cases: no selection, multi-select, and flicker when the control is updated. It also ensures the newly selected item is scrolled into view.

if (listView1.Items.Count == 0) return;

int current = listView1.SelectedIndices.Count > 0 ? listView1.SelectedIndices[0] : -1;
int next = (current + 1) % listView1.Items.Count; // change logic if wrap-around is not wanted

listView1.BeginUpdate();
try
{
    // clear existing selection safely
    while (listView1.SelectedItems.Count > 0)
        listView1.SelectedItems[0].Selected = false;

    listView1.Items[next].Selected = true;
    listView1.FocusedItem = listView1.Items[next];
    listView1.EnsureVisible(next);   // scrolls the item into view
    listView1.Focus();
}
finally { listView1.EndUpdate(); }

Notes and troubleshooting:

  • To avoid wrap-around, replace the next calculation with int next = Math.Min(current + 1, listView1.Items.Count - 1); and optionally return early if current == next.
  • If multiple items should remain selected, skip the loop that clears selection and adjust logic accordingly.
  • If selection highlight disappears when the control loses focus, set listView1.HideSelection = false.
  • In VirtualMode or owner-drawn scenarios, selection/visibility must be managed with VirtualListSize and the virtual item events. Also ensure all UI updates run on the UI thread (check InvokeRequired/Invoke).

This keeps the interaction smooth and avoids the SelectedIndices[0] exception when nothing is selected, while ensuring the chosen item is visible.

Recommended Answers

All 2 Replies

int current = this.listView1.SelectedIndices[0];
int next = current == this.listView1.Items.Count - 1 ? 0 : current + 1;
this.listView1.Focus();
this.listView1.Items[next].Selected = true;
this.listView1.Items[current].Selected = false;

note: this will jump back to the first one when the last one was selected

commented: Thanks, very helpful +1
int current = this.listView1.SelectedIndices[0];
int next = current == this.listView1.Items.Count - 1 ? 0 : current + 1;
this.listView1.Focus();
this.listView1.Items[next].Selected = true;
this.listView1.Items[current].Selected = false;

note: this will jump back to the first one when the last one was selected

thanks that worked. And to make it go the other way is a very simple tweak :)
Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.