Migrating From ListBox to ListView: Only One Problem

T

The Confessor

I've nearly completed the migration of my first ListBox control to a
ListView in detail view, and I'm only experiencing one problem:

My ListBox.SelectedIndexChanged procedure began as follows:

NextPointID = ListBox_Points.SelectedIndex + 1

The equivalent of that command for a ListView would seem to be:

NextPointID = ListView_Points.SelectedIndices(0) + 1

Since my ListView doesn't allow multiselect, the first (0th) SelectedIndex
in the collection would always be the only member, correct?

A single execution of that line of code works perfectly, but if I attempt
to choose another index, it returns the following error:

---

An unhandled exception of type 'System.ArgumentOutOfRangeException'
occurred in system.windows.forms.dll

Additional information: Specified argument was out of the range of valid
values.

---

So, what's the sitch? What'd I do wrong?

Thanks in advance for any assistance,

The Confessor
 
T

tomb

You said the answer yourself - you don't allow multi-select, so you
can't use a different index.

Tom
 
T

The Confessor

You said the answer yourself - you don't allow multi-select, so you
can't use a different index.

Tom

Are you certain you understood the question I was asking? I've
intentionally set MultiSelect to false.

What I want is a way to pass the index of the sole selected item + 1 to the
NextPointID variable.

My current code will do this only once, crashing *on that line* on the
second iteration.
 
A

Armin Zingler

The Confessor said:
I've nearly completed the migration of my first ListBox control to a
ListView in detail view, and I'm only experiencing one problem:

My ListBox.SelectedIndexChanged procedure began as follows:

NextPointID = ListBox_Points.SelectedIndex + 1

The equivalent of that command for a ListView would seem to be:

NextPointID = ListView_Points.SelectedIndices(0) + 1

Since my ListView doesn't allow multiselect, the first (0th)
SelectedIndex in the collection would always be the only member,
correct?

A single execution of that line of code works perfectly, but if I
attempt to choose another index, it returns the following error:

---

An unhandled exception of type 'System.ArgumentOutOfRangeException'
occurred in system.windows.forms.dll

Additional information: Specified argument was out of the range of
valid values.


Add this as the first line in SelectedIndexChanged:

if listbox_points.selectedindices.count = 0 then return

Reason: If you select another item, the previous item is un-selected and
SelectedIndexChanged is raised. Count = 0 in this case.


Armin
 
T

The Confessor

Add this as the first line in SelectedIndexChanged:

if listbox_points.selectedindices.count = 0 then return

Reason: If you select another item, the previous item is un-selected and
SelectedIndexChanged is raised. Count = 0 in this case.


Armin

!

Took me a few minutes to see what you were saying:

Whereas selecting a different ListBox entry raises the SelectedIndexChanged
event only once, selecting a different ListView entry raises it twice. Once
for the de-selection of the previous index, and one for the selection of
the new.

This requires a bail-out statement in case of the former prior to any tests
of SelectedIndice content.

A hellishly devious change.

I am in your debt, Armin.

The Confessor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top