Home All Groups Group Topic Archive Search About

Migrating From ListBox to ListView: Only One Problem

Author
13 Jan 2006 3:46 PM
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

Author
13 Jan 2006 4:01 PM
tomb
You said the answer yourself - you don't allow multi-select, so you
can't use a different index.

Tom

The Confessor wrote:

Show quoteHide quote
>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

>
Author
13 Jan 2006 4:22 PM
The Confessor
tomb <t***@technetcenter.com> wrote in
news:v8Qxf.37244$qw4.12007@bignews5.bellsouth.net:

> 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.
Author
13 Jan 2006 6:00 PM
Armin Zingler
Show quote Hide quote
"The Confessor" <inva***@reply.to.group> schrieb
> 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?


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
Author
13 Jan 2006 7:07 PM
The Confessor
"Armin Zingler" <az.nospam@freenet.de> wrote in news:O75zwxGGGHA.2300
@TK2MSFTNGP15.phx.gbl:

> 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