Remove items form ListView and Treeview

G

Guest

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
 
C

Cor Ligthert

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
 
H

Herfried K. Wagner [MVP]

Progalex said:
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
///
 
G

Guest

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
 
H

Herfried K. Wagner [MVP]

Progalex said:
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()
///
 
G

Guest

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
 
A

Atara

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

Guest

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
 

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

Similar Threads


Top