PC Review Forums Newsgroups Microsoft DotNet Microsoft VB .NET Migrating From ListBox to ListView: Only One Problem

Reply

Migrating From ListBox to ListView: Only One Problem

 
Thread Tools Rate Thread
Old 13-01-2006, 03:46 PM   #1
The Confessor
Guest
 
Posts: n/a
Default Migrating From ListBox to ListView: Only One Problem


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
  Reply With Quote
Old 13-01-2006, 04:01 PM   #2
tomb
Guest
 
Posts: n/a
Default Re: Migrating From ListBox to ListView: Only One Problem

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

Tom

The Confessor wrote:

>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
>
>

  Reply With Quote
Old 13-01-2006, 04:22 PM   #3
The Confessor
Guest
 
Posts: n/a
Default Re: Migrating From ListBox to ListView: Only One Problem

tomb <tomb@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.
  Reply With Quote
Old 13-01-2006, 06:00 PM   #4
Armin Zingler
Guest
 
Posts: n/a
Default Re: Migrating From ListBox to ListView: Only One Problem

"The Confessor" <invalid@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

  Reply With Quote
Old 13-01-2006, 07:07 PM   #5
The Confessor
Guest
 
Posts: n/a
Default Re: Migrating From ListBox to ListView: Only One Problem

"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
  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off