locked
Why can't I add TreeNode to the TreeView? RRS feed

  • Question

  • I have such code

    TreeNode added = treeView1.SelectedNode.Nodes.Add("test");
    added.ImageIndex = 0;
    added.SelectedImageIndex = 0;
    treeView1.Refresh();
    treeView1.Invalidate();
    

    But new node with name "test" doesn't become visible.

    Why?

    Friday, October 8, 2010 10:35 AM

Answers

  • Hi,

    I've copied your code and tried, there is no problem in your code. Its adding new node to the currently selected tree node. So the problem is that newly added node was not visible. Am I correct?If yes, Add the following line along with your existing code 

    added.EnsureVisible(); 
    
    

    Thanks and Regards, Bharath S.
    • Proposed as answer by Morten Wennevik Friday, October 8, 2010 1:34 PM
    • Marked as answer by Alan_chen Friday, October 15, 2010 1:25 AM
    Friday, October 8, 2010 12:41 PM

All replies

  •       TreeNode treeNode = new TreeNode("Test");
          treeView1.Nodes.Add(treeNode);
    

    G.

    Friday, October 8, 2010 10:42 AM
  • Dear All,

    The problem is on which event you want to show the node. As per the scenario, this should be in the AfterSelect event of the TreeView class. Please refer to the below code:

    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
                TreeNode node = new TreeNode("test", 0, 0);
                treeView1.SelectedNode.Nodes.Add(node);

    }

    Now after selecting the node, the required node will be added.

    • Proposed as answer by Satya Jugran Friday, October 8, 2010 11:07 AM
    Friday, October 8, 2010 10:59 AM
  • Hi,

    I've copied your code and tried, there is no problem in your code. Its adding new node to the currently selected tree node. So the problem is that newly added node was not visible. Am I correct?If yes, Add the following line along with your existing code 

    added.EnsureVisible(); 
    
    

    Thanks and Regards, Bharath S.
    • Proposed as answer by Morten Wennevik Friday, October 8, 2010 1:34 PM
    • Marked as answer by Alan_chen Friday, October 15, 2010 1:25 AM
    Friday, October 8, 2010 12:41 PM