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

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)

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

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.

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

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

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

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.

It worked. Thank you so much !!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.