Click to See Complete Forum and Search --> : TreeView.Insert() problem


sternaphile
July 22nd, 2002, 01:32 PM
I'm using TreeView in an app, and I'm trying to Insert a new node into a specific area in the tree. I'm trying to insert it under it's parent, but it keeps being inserted at a level equal to the 'root'.
What am I doing wrong??


.
.
.
// Get currently selected node and make a new name for child.
TreeNode ThisNode = LeftTreeView.SelectedNode;
String str2 = ThisNode.Text + "_child";

// Create a new node.
TreeNode NewNode = new TreeNode(str2);

// Get index of parent node.
int nIdx = -1;
nIdx = ThisNode.Index;

// Bump index and insert NewNode directly under parent.
LeftTreeView.Nodes.Insert(nIdx+1, NewNode);
.
.
.

Thanks for your help
:confused: :confused: :confused: :confused: :confused: :confused:

Arild Fines
July 22nd, 2002, 02:26 PM
The TreeView control doesnt quite work that way. If you want a node to be a child of another, you have to explicitly add it as such. You don't mess around with numerical indices.

// Get currently selected node and make a new name for child.
TreeNode ThisNode = LeftTreeView.SelectedNode;
String str2 = ThisNode.Text + "_child";

// Create a new node.
TreeNode NewNode = new TreeNode(str2);

ThisNode.Nodes.Add( NewNode );

sternaphile
July 22nd, 2002, 02:31 PM
Arild,
Thanks a heap!!