CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2001
    Location
    Midwest
    Posts
    57

    Angry TreeView.Insert() problem

    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
    "Judge a man by his questions rather than his answers." - Voltaire

  2. #2
    Join Date
    Oct 2001
    Location
    Norway
    Posts
    265
    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.
    Code:
    // 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 );

  3. #3
    Join Date
    Apr 2001
    Location
    Midwest
    Posts
    57

    Thumbs up

    Arild,
    Thanks a heap!!
    "Judge a man by his questions rather than his answers." - Voltaire

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured