954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to delete item in ListView and TreeView

Hello Everybody !!!

I have a problem about TreeView and ListView.

I have a form to manage student. When I add, the data appear on ListView and TreeView at the same time and the same data. Everything works perfectly.

But one proble about Delete button. I choose the line from the ListView and Delete it. The ListView delete the item perfectly. But I don't know how to delete the item on ListView and automatically delete the item on the TreeView.

Thanks for your helping !!!

Note: When you insert the data to ListView and TreeView. When you fill the information into the textbox. Remember after that choose class on TreeView and Click BUTTON ADD.

SOURCE CODE

Example.zip

Attachments Example.zip (79.17KB)
nndung179
Light Poster
25 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

If the item has been added at the same place in both the TreeView and the ListView, then you can make a note of the index of the selected item in the ListView before you delete it.
You can then use that index to locate the corresponding node in the TreeView and delete that node.

Dim index As Integer
index = ListView1.SelectedIndices.Item(0)
ListView.SelectedItems.Item(0).Remove()

TreeView.Nodes.RemoveAt(index)
Oxiegen
Master Poster
715 posts since Jun 2006
Reputation Points: 87
Solved Threads: 141
 

The Node I want to delete is the child node. Take a look with my demo, you will see. I do exactly as you do. But It deleted my Parent node not the child node inside of it.

Thanks for replying !!!

nndung179
Light Poster
25 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

I see your point.
Then a slight change might be in order. Try this:

Dim key As String
Dim arrUser As ListView.SelectedListViewItemCollection
arrUser = liststudentListView.SelectedItems

For Each item As ListViewItem In arrUser
    key = item.Text
    liststudentListView.Items.Remove(item)
    liststudentTreeView.Nodes.RemoveByKey(key)
Next

This should remove the treenode containing the exact same student code as the item in the listview.

Oxiegen
Master Poster
715 posts since Jun 2006
Reputation Points: 87
Solved Threads: 141
 

It doesn't work. I delete only the item in ListView.

nndung179
Light Poster
25 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Alright.
RemoveByKey seems to only remove nodes from the position in the tree where you call it.
Replace the line "liststudentTreeView.Nodes.RemoveByKey(key)" with this:

Dim node() As TreeNode = liststudentTreeView.Nodes.Find(key, True)
liststudentTreeView.Nodes.Remove(node(0))
Oxiegen
Master Poster
715 posts since Jun 2006
Reputation Points: 87
Solved Threads: 141
 

The error MesseageBox say "Additional information: Index was outside the bounds of the array." I've never use this before. Please help !!!

Dim node() As TreeNode = liststudentTreeView.Nodes.Find(key, True)
            liststudentTreeView.Nodes.Remove(node(0))
nndung179
Light Poster
25 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Ok. I managed to get it to work.
Use the codes I gave you, but add one single line to the Student class.

In the method Add_Node_Student, add the line "kq.Name = sv._CODE".

The Find(key, True) method is looking for the NAME of the node.

Oxiegen
Master Poster
715 posts since Jun 2006
Reputation Points: 87
Solved Threads: 141
 

It worked. Thank you so much !!!

nndung179
Light Poster
25 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You