In my treeview I want to remember which nodes were expanded and redraw that state after I delete or insert a node. I solved that with remembering the index of an expanded node. If a node is removed, I decrement all indeces that were afterwards. That works because I can access the deleted node with treeView.SelectedNode, but how can I get the new Index of the inserted node? I can not solve it with saving a reference to the node or a name or a tag, because when I redraw the tree the nodes are created completely new - and I think I can not get a reference to the newly created node anyway Best regards, Expecto
Asked
Active
Viewed 1,743 times
3 Answers
0
My way: use your own declared class:
class TreeNodeEx : TreeNode
{
public void Remove()
{
base.Remove();
// what you want to do
UpdateNode(this.Parent);
}
}

Ria
- 10,237
- 3
- 33
- 60
0
You could bind an observableCollection to your TreeNode Then :
var obd = Observable.FromEvent<NotifyCollectionChangedEventArgs>(
ev => obdCollection.CollectionChanged += CheckChanges,ev=> obdCollection.CollectionChanged -= CheckChanges);
private void CheckChanges(object sender, NotifyCollectionChangedEventArgs e)
{
Console.WriteLine("new Starting index : "+e.NewStartingIndex);
Console.WriteLine("Old Starting index : " + e.OldStartingIndex);
Console.WriteLine("new Items : " + e.NewItems);
Console.WriteLine("Old Items : " + e.OldItems);
}
That way you can check what has changed in the collection.

Fx Mzt
- 381
- 3
- 5
0
The answers were not really what I was looking for, but after I found out that after an insert the "AfterSelect" Event fires, I did it this way:
private void treeViewProduct_AfterSelect(object sender, TreeViewEventArgs e)
{
if (insertMode)
{
treeViewProduct.NotifyAboutInsert(e.Node.Index);
}
...
}
My treeview then handles the changed indices for the nodes after that and perfectly redraws the tree.

Verena Haunschmid
- 1,252
- 15
- 40