1.11M Members

How to delete item in ListView and TreeView

 
0
 

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

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

 
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.

 
0
 

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

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

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

 
0
 

It worked. Thank you so much !!!

Question Answered as of 2 Years Ago by Oxiegen
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article