This is a split board - You can return to the Split List for other boards.

Can anyone help me with a C# problem I'm having?

#1Knighted DragonPosted 11/23/2013 10:27:42 AM
I have a tree view and have a remove button, and I want the user to be able to click on a node and remove the data from the parent and all child nodes if applicable, and I also need it to remove the data from the collection that holds the data that filled the tree node.

treeView1.Items.Remove(treeView1.SelectedItem);


That removes the node itself from being visible, but the data is still in the collection. Thus far I've been having a really hard time trying to get the selected index to remove the parent node and all the child nodes as well from the data, does anyone know how to do it? I can upload a picture of the program if it would help
---
Matthew 7:21
http://img15.imageshack.us/img15/2492/gyenyame.jpg
#2capgamerPosted 11/23/2013 10:45:06 AM(edited)
I don't know much of anything about C#, but it sounds like you need to set up like three pointers, "head node" - the top of the section you want to delete, "previous node" and "current node."

You want to iterate through the tree starting from the head node until you reach the "leftmost" lowest child node. Here your previous node will point to the parent of that lowest child node, your current node will point to the lowest child node. Remove the current node, then reset to the previous node.

When you run out of options, reset to the header node until the header node has no lower nodes beneath it. Then delete the header.

And then you iterate a bunch like that until you've removed each child node, resetting when you can't go any lower in the tree.
---
The man keeps us down
because he likes to see us frown
#3SinisterSlayPosted 11/23/2013 10:44:30 AM
I'm surprised it does that. But you could recursively loop through the children remove them.
---
He who stumbles around in darkness with a stick is blind. But he who... sticks out in darkness... is... fluorescent! - Brother Silence
#4Cush1978Posted 11/23/2013 2:27:40 PM
It sounds like you're approaching this backwards. Your tree view is a visual representation of your data. You should remove the selected node from your data, then tell the tree view to refresh/update itself.

- Figure out the selected node in the tree view.
- Find the right item in your data collection (and children).
- Remove the right item from your data collection.
- Refresh tree view.
---
Cush - Wii Friend Code: 8824 6045 3862 1803
#5Knighted Dragon(Topic Creator)Posted 11/23/2013 2:32:37 PM
From: Cush1978 | #004
- Find the right item in your data collection (and children).
- Remove the right item from your data collection.



These are the two that are tripping me up. Even if I get the selected index that the user clicks on, it doesn't tell me how many children it has, and I have no way to know how many children the node will have in it because the user can enter as many as they want. I don't know any way to write a for each that can go down through the children removing them and any children they might have without clearing the entire collection =/
---
Matthew 7:21
http://img15.imageshack.us/img15/2492/gyenyame.jpg
#6SinisterSlayPosted 11/25/2013 9:31:26 AM(edited)
Knighted Dragon posted...
From: Cush1978 | #004
- Find the right item in your data collection (and children).
- Remove the right item from your data collection.



These are the two that are tripping me up. Even if I get the selected index that the user clicks on, it doesn't tell me how many children it has, and I have no way to know how many children the node will have in it because the user can enter as many as they want. I don't know any way to write a for each that can go down through the children removing them and any children they might have without clearing the entire collection =/


If your collection is built correctly, it should automatically dump the children when you remove the parent.
When you say collection, what are you referring to? You have a collection of objects? Or did you make a matrix?
---
He who stumbles around in darkness with a stick is blind. But he who... sticks out in darkness... is... fluorescent! - Brother Silence