473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Removing multiple items from a multi-select ListView

(VB'2003)
What's the correct way to remove multiple, selected items from a
ListView control (say, from a ContextMenu)?

I ask because I'm getting a very annoying ArgumentOutOfRangeException
because the ListView seems to be trying to "re-select" items that are no
longer there!

for example, giventhat I have 3 items in my list:
Select the first and remove it - no problem.
Select the last /but one/ and remove it - still no problem.
Select the /last/ item and remove it - Boom!

ArgumentOutOfRangeException
Parameter name: '2' is not a valid value for 'displayIndex'
at S.W.F.ListViewItemCollection.getItem(Int32 displayIndex)
at S.W.F.ListView.LvnBeginDrag(MouseButtons buttons, NMLISTVIEW mnlv)
...

It's as if the "list" of select indices is being re-applied, even though
the items to which those items correspond[ed] have been removed.

Any suggestions?

[code]
Dim iIndices As Integer()
ReDim iIndices(Me.lvMembers.SelectedIndices.Count - 1)
Me.lvMembers.SelectedIndices.CopyTo(iIndices, 0)

For iIndex As Integer = iIndices.Length - 1 To 0 Step -1
Dim oItem As ListViewItem _
= Me.lvMembers.Items(iIndices(iIndex))

oItem.Selected = False ' even tried this!
Me.lvMembers.Items.Remove(oItem)
Next

TIA,
Phill W.
Jul 27 '06 #1
5 10047
Why do you select the item to remove it
Get the index of the item you want to remove and use the
ListView.Items.RemoveAt method

hth,
Samuel

"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message
news:ea**********@south.jnrs.ja.net...
(VB'2003)
What's the correct way to remove multiple, selected items from a ListView
control (say, from a ContextMenu)?

I ask because I'm getting a very annoying ArgumentOutOfRangeException
because the ListView seems to be trying to "re-select" items that are no
longer there!

for example, giventhat I have 3 items in my list:
Select the first and remove it - no problem.
Select the last /but one/ and remove it - still no problem.
Select the /last/ item and remove it - Boom!

ArgumentOutOfRangeException
Parameter name: '2' is not a valid value for 'displayIndex'
at S.W.F.ListViewItemCollection.getItem(Int32 displayIndex)
at S.W.F.ListView.LvnBeginDrag(MouseButtons buttons, NMLISTVIEW mnlv)
...

It's as if the "list" of select indices is being re-applied, even though
the items to which those items correspond[ed] have been removed.

Any suggestions?

[code]
Dim iIndices As Integer()
ReDim iIndices(Me.lvMembers.SelectedIndices.Count - 1)
Me.lvMembers.SelectedIndices.CopyTo(iIndices, 0)

For iIndex As Integer = iIndices.Length - 1 To 0 Step -1
Dim oItem As ListViewItem _
= Me.lvMembers.Items(iIndices(iIndex))

oItem.Selected = False ' even tried this!
Me.lvMembers.Items.Remove(oItem)
Next

TIA,
Phill W.

Jul 27 '06 #2
Samuel,
Why do you select the item to remove it
I don't - I'm trying to "un-select" it in the vain hope that the
ListView control won't then try to redisplay it in a "selected" way (or
/any/ way, come to that), which is what's causing it to blow up.
Get the index of the item you want to remove and use the
ListView.Items.RemoveAt method
Yep - tried that. Same results using Remove(item) and RemoveAt(index).

Regards,
Phill W.

Samuel Shulman wrote:
Why do you select the item to remove it
Get the index of the item you want to remove and use the
ListView.Items.RemoveAt method

hth,
Samuel

"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message
news:ea**********@south.jnrs.ja.net...
>(VB'2003)
What's the correct way to remove multiple, selected items from a ListView
control (say, from a ContextMenu)?

I ask because I'm getting a very annoying ArgumentOutOfRangeException
because the ListView seems to be trying to "re-select" items that are no
longer there!

for example, giventhat I have 3 items in my list:
Select the first and remove it - no problem.
Select the last /but one/ and remove it - still no problem.
Select the /last/ item and remove it - Boom!

ArgumentOutOfRangeException
Parameter name: '2' is not a valid value for 'displayIndex'
at S.W.F.ListViewItemCollection.getItem(Int32 displayIndex)
at S.W.F.ListView.LvnBeginDrag(MouseButtons buttons, NMLISTVIEW mnlv)
...

It's as if the "list" of select indices is being re-applied, even though
the items to which those items correspond[ed] have been removed.

Any suggestions?

[code]
Dim iIndices As Integer()
ReDim iIndices(Me.lvMembers.SelectedIndices.Count - 1)
Me.lvMembers.SelectedIndices.CopyTo(iIndices, 0)

For iIndex As Integer = iIndices.Length - 1 To 0 Step -1
Dim oItem As ListViewItem _
= Me.lvMembers.Items(iIndices(iIndex))

oItem.Selected = False ' even tried this!
Me.lvMembers.Items.Remove(oItem)
Next

TIA,
Phill W.

Jul 27 '06 #3
Phill W. wrote:
(VB'2003)
What's the correct way to remove multiple, selected items from a
ListView control (say, from a ContextMenu)?

I ask because I'm getting a very annoying ArgumentOutOfRangeException
because the ListView seems to be trying to "re-select" items that are no
longer there!

for example, giventhat I have 3 items in my list:
Select the first and remove it - no problem.
Select the last /but one/ and remove it - still no problem.
Select the /last/ item and remove it - Boom!

ArgumentOutOfRangeException
Parameter name: '2' is not a valid value for 'displayIndex'
at S.W.F.ListViewItemCollection.getItem(Int32 displayIndex)
at S.W.F.ListView.LvnBeginDrag(MouseButtons buttons, NMLISTVIEW mnlv)
...

It's as if the "list" of select indices is being re-applied, even though
the items to which those items correspond[ed] have been removed.

Any suggestions?

[code]
Dim iIndices As Integer()
ReDim iIndices(Me.lvMembers.SelectedIndices.Count - 1)
Me.lvMembers.SelectedIndices.CopyTo(iIndices, 0)

For iIndex As Integer = iIndices.Length - 1 To 0 Step -1
Dim oItem As ListViewItem _
= Me.lvMembers.Items(iIndices(iIndex))

oItem.Selected = False ' even tried this!
Me.lvMembers.Items.Remove(oItem)
Next

TIA,
Phill W.
At the risk of seeming to talk to myself (not a first, by any means) it
looks like the SelectedIndices might be a Red Herring.

I /only/ get this problem if the item corresponding to the last item I
selected (by Clicking and Ctrl-Clicking in the ListView) is removed, so...
If I select the last item in the list, then the first, I can remove them
both and the first item remains selected.
If I select the first item, then the last, and remove them - Boom!

Regards,
Phill W.
Jul 27 '06 #4
Maybe that,
When you actually remove an item then the index of the other items changes
and if say the maximum index value was 10 after removing any item it will be
9

You can do 1 of the following:
1. Record to some variables (array) a unique value of the items you want to
remove and find out the index of that item each time based on that value
then usse the removeAt

2. Reference all the items to remove to a created array of items then loop
through this array (I hope that the valuue of the index will change as you
go along)
hth,
Samuel Shulman

"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message
news:ea**********@south.jnrs.ja.net...
Samuel,
Why do you select the item to remove it
I don't - I'm trying to "un-select" it in the vain hope that the ListView
control won't then try to redisplay it in a "selected" way (or /any/ way,
come to that), which is what's causing it to blow up.
Get the index of the item you want to remove and use the
ListView.Items.RemoveAt method

Yep - tried that. Same results using Remove(item) and RemoveAt(index).

Regards,
Phill W.

Samuel Shulman wrote:
>Why do you select the item to remove it
Get the index of the item you want to remove and use the
ListView.Items.RemoveAt method

hth,
Samuel

"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message
news:ea**********@south.jnrs.ja.net...
>>(VB'2003)
What's the correct way to remove multiple, selected items from a
ListView control (say, from a ContextMenu)?

I ask because I'm getting a very annoying ArgumentOutOfRangeException
because the ListView seems to be trying to "re-select" items that are no
longer there!

for example, giventhat I have 3 items in my list:
Select the first and remove it - no problem.
Select the last /but one/ and remove it - still no problem.
Select the /last/ item and remove it - Boom!

ArgumentOutOfRangeException
Parameter name: '2' is not a valid value for 'displayIndex'
at S.W.F.ListViewItemCollection.getItem(Int32 displayIndex)
at S.W.F.ListView.LvnBeginDrag(MouseButtons buttons, NMLISTVIEW mnlv)
...

It's as if the "list" of select indices is being re-applied, even though
the items to which those items correspond[ed] have been removed.

Any suggestions?

[code]
Dim iIndices As Integer()
ReDim iIndices(Me.lvMembers.SelectedIndices.Count - 1)
Me.lvMembers.SelectedIndices.CopyTo(iIndices, 0)

For iIndex As Integer = iIndices.Length - 1 To 0 Step -1
Dim oItem As ListViewItem _
= Me.lvMembers.Items(iIndices(iIndex))

oItem.Selected = False ' even tried this!
Me.lvMembers.Items.Remove(oItem)
Next

TIA,
Phill W.
Jul 27 '06 #5
Samuel,

Found it!

Old habits die hard: I was displaying the Context Menu manually (using
ContextMenu.Show) but from the ListView's Mouse/Down/ event instead of
Mouse/Up/.

Moved my code into[ListView].MouseUp and everything works as expected!

Regards,
Phill W.

Samuel Shulman wrote:
Maybe that,
When you actually remove an item then the index of the other items changes
and if say the maximum index value was 10 after removing any item it will be
9

You can do 1 of the following:
1. Record to some variables (array) a unique value of the items you want to
remove and find out the index of that item each time based on that value
then usse the removeAt

2. Reference all the items to remove to a created array of items then loop
through this array (I hope that the valuue of the index will change as you
go along)
hth,
Samuel Shulman

"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message
news:ea**********@south.jnrs.ja.net...
>Samuel,
>>Why do you select the item to remove it
I don't - I'm trying to "un-select" it in the vain hope that the ListView
control won't then try to redisplay it in a "selected" way (or /any/ way,
come to that), which is what's causing it to blow up.
>>Get the index of the item you want to remove and use the
ListView.Items.RemoveAt method
Yep - tried that. Same results using Remove(item) and RemoveAt(index).

Regards,
Phill W.

Samuel Shulman wrote:
>>Why do you select the item to remove it
Get the index of the item you want to remove and use the
ListView.Items.RemoveAt method

hth,
Samuel

"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message
news:ea**********@south.jnrs.ja.net...
(VB'2003)
What's the correct way to remove multiple, selected items from a
ListView control (say, from a ContextMenu)?

I ask because I'm getting a very annoying ArgumentOutOfRangeException
because the ListView seems to be trying to "re-select" items that are no
longer there!

for example, giventhat I have 3 items in my list:
Select the first and remove it - no problem.
Select the last /but one/ and remove it - still no problem.
Select the /last/ item and remove it - Boom!

ArgumentOutOfRangeException
Parameter name: '2' is not a valid value for 'displayIndex'
at S.W.F.ListViewItemCollection.getItem(Int32 displayIndex)
at S.W.F.ListView.LvnBeginDrag(MouseButtons buttons, NMLISTVIEW mnlv)
...

It's as if the "list" of select indices is being re-applied, even though
the items to which those items correspond[ed] have been removed.

Any suggestions?

[code]
Dim iIndices As Integer()
ReDim iIndices(Me.lvMembers.SelectedIndices.Count - 1)
Me.lvMembers.SelectedIndices.CopyTo(iIndices, 0)

For iIndex As Integer = iIndices.Length - 1 To 0 Step -1
Dim oItem As ListViewItem _
= Me.lvMembers.Items(iIndices(iIndex))

oItem.Selected = False ' even tried this!
Me.lvMembers.Items.Remove(oItem)
Next

TIA,
Phill W.
Jul 28 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jeremy Owens-Boggs | last post by:
We are trying to implement a dual list box selection where you have two list boxes, You highlight items in the right side list box, click a button and this moves those items over to the left hand...
2
by: Chris | last post by:
I find tons of info on adding, but what about removing an item dynamically? I'm cycling through the list and removing users that have special rights. I've tried things like: ...
3
by: Arun | last post by:
Hi, I have simple question to ask. How to write multiple Binary files to the Browser using Asp.Net and Visual C#.net I have seen examples where single binary file is written to browser. ...
2
by: Milsnips | last post by:
hi there, i have the following HTML code: -------------------------- <asp:DropDownList id="hddList" runat="server"> <asp:ListItem Value="1">Item 1</asp:ListItem> <asp:ListItem Value="2">Item...
10
by: Backwards | last post by:
Hello all, I'll start by explaining what my app does so not to confuss you when i ask my question. ☺ I have a VB.Net 2.0 app that starts a process (process.start ...) and passes a prameter...
4
neo008
by: neo008 | last post by:
Hi all, Finally gave up and putting it here. I am new to visual basic stucked up with an error- Run time errors.'-2147217887 (8004021)': Multiple-step operation generated errors. check each...
2
by: beatTheDevil | last post by:
Hey guys, As the title says I'm trying to make a regular expression (regex/regexp) for use in removing the comments from code. In this case, this particular regex is meant to match /* ... */...
1
by: robinthezhu | last post by:
Hello, I recently ported my code to Tornado 2.2 VxWorks5.5 I finaly got all the projects to build and compile,but in the last stage of the build when I link the application software with the ppc...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
0
by: Scott David Daniels | last post by:
Here are some tweaks on both bits of code: Paul McGuire wrote: .... m = for b in bases: if hasattr(b, '__mro__'): if MetaHocObject.ho in b.__mro__: m.append(b) if m:
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.