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
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
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:
next calculation with int next = Math.Min(current + 1, listView1.Items.Count - 1); and optionally return early if current == next.listView1.HideSelection = false.This keeps the interaction smooth and avoids the SelectedIndices[0] exception when nothing is selected, while ensuring the chosen item is visible.
Jump to Post— r0ckbaer 3int 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
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
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
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.