Valhalla Legends Forums Archive | General Programming | Re: Another C# Q

AuthorMessageTime
Noodlez
I have some nodes in a TreeView...I populate them.
After they're all populated I add another node, "Offline." I need to move every item under the other Nodes into my final Offline node. Any suggestions?
November 7, 2007, 6:44 AM
Myndfyr
Iterate over them, remove them from their parents, and set their new parents to the offline node?

Really shouldn't be terribly difficult....
November 7, 2007, 9:28 AM
Noodlez
Parent is a read only property
November 7, 2007, 9:00 PM
Noodlez
It's ok, I did the iteration Remove and Insert loop.
November 7, 2007, 9:28 PM
Myndfyr
[quote author=Noodlez link=topic=17155.msg174678#msg174678 date=1194469222]
Parent is a read only property
[/quote]
Assuming:
Root
- Buddies
--MyndFyre
--Noodlez
-Co-workers
--Justin
--Ryan
--Jeff
--Steve
-Offline
[code]
int currentIndex = 0;
TreeNode offlineNode = whatever; // you need to have a reference to the offline node for the following code to work
// this code also assumes that you have one root node.  You'll have to adapt it for multiple.
while (currentIndex < rootNode.ChildNodes.Count)
{
 TreeNode currentNode = rootNode.ChildNodes[currentIndex];
 if (currentNode != offlineNode)
 {
   rootNode.ChildNodes.Remove(currentNode);
   offlineNode.ChildNodes.Add(currentNode);
 }
 else
 {
   currentIndex++;
 }
}
[/code]

You can't use a foreach because you're going to change the collection you're iterating over while you're looping, so you have to use a for, while, or do-while.  A for loop is inappropriate because you don't necessarily want to increment each time.
November 7, 2007, 9:29 PM

Search