By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,995 Members | 1,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,995 IT Pros & Developers. It's quick & easy.

Remove items form ListView and Treeview

P: n/a

Hi everybody!

I have a listview and a treeview in a form . With an OpenDialog I let the
user select multiple files and then these files are added to the listview
with the complete pathname, while they are added to the treeview without the
path (only file name). I'd like the user would be able to remove multiple
items selecting filenames from the listview.
So, items should be removed both from the listview and the treeview.
How can I do this? For the listview I'm using the following code:

ListView1.Items.RemoveAt(ListView1.SelectedItems(0 ).Index())

but it just removes one item, and not multiple selection!
While for the treeview I'm trying some snippet but I can't get any good
result.

Any help or link to any sample would be appreciated..

Progalex
Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Probalex.

When you can do this in VBNet
ListView1.Items.RemoveAt(ListView1.SelectedItems(0 ).Index())

You can do forever
\\\\
for i as integer = Listview1.selecteditems.count -1 to 0 step -1
ListView1.Items.RemoveAt(ListView1.SelectedItems(i ).Index())
next
///
There are more ways by instance,
\\\\
for i as integer = Listview1.selecteditems.count -1 to 0 step -1
ListView1.Items(i).Remove
next
///
However what I want to show you is that when you are deleting from a
colletion the best way to do it is almost forever bottom up.

All typed in this message so watch typos or other errors.

I hope this helps?

Cor
Nov 21 '05 #2

P: n/a
"Progalex" <Pr******@discussions.microsoft.com> schrieb:
So, items should be removed both from the listview
and the treeview. How can I do this? For the listview
I'm using the following code:

ListView1.Items.RemoveAt(ListView1.SelectedItems(0 ).Index())

but it just removes one item, and not multiple selection!


\\\
With Me.ListView1
For i As Integer = 0 To .SelectedItems.Count - 1
.Items.Remove(.SelectedItems(0))
Next i
End With
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #3

P: n/a


Ok, both the answers above was good to me! Thanks.

But..does a treeview control work in the same way? I mean, about removing
items from a treeview.

Thanks a lot!

Progalex
Nov 21 '05 #4

P: n/a
"Progalex" <Pr******@discussions.microsoft.com> schrieb:
But..does a treeview control work in the same way?
I mean, about removing items from a treeview.


You can remove the selected node:

\\\
Me.TreeView1.SelectedNode.Remove()
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #5

P: n/a
Ok, now I'm trying to do something more.

Since files chosen from an OpenFile dialog are simultaneously added to a
listview and a treeview, to remove them I'm using the following code:

With Me.ListView1
For I = 0 To .SelectedItems.Count - 1

.Items.Remove(.SelectedItems(0))

TreeView1.Nodes(0).Nodes(I).Remove()

Next (I)
End With

But I think there's something wrong: from listview items are correctly
removed, but in the treeview only the first item is removed exactly, while
the others are not removed in the right order. I tried to modify the 'I'
variable value, (e.g. adding 1 or -1...) but it doesn't work.
Can you find my mistake inside this code?

Thanks,
Progalex
Nov 21 '05 #6

P: n/a
...when you are deleting from a
colletion the best way to do it is almost forever bottom up...
since you are changing the items collection during the iteration on the
same collection.
If you chose to do it [0 to Count - 1], you always should delete the [0]
item
With Me.ListView1
For I = 0 To .SelectedItems.Count - 1
.Items.Remove(.SelectedItems(0))
TreeView1.Nodes(0).Nodes(0).Remove() ' <- Note: 0 not I!
Next (I)
End With

Atara.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #7

P: n/a


Tried... but it doesn't work the same!

I'm going slightly mad!

Could it be that Treeview nodes haven't been programmatically selected? Is
there anyway to do this?

Thanks again...
Progalex
Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.