none
selecting nodes in a treeview RRS feed

  • Question

  • Hi.

    I have a tree view and I'm trying to select the parent node of an especific node when it is selected. here ins my tree structure:

    node1
        node1.1
        node1.2
            node 1.2.1
            node 1.2.2
        node1.3
    node2
    .
    .
    .

    For example: when the node 1.2.2 is selected I want to select also its parent node (node 1.2) and also that one parent (node 1).

    Thanks!!
    Monday, October 13, 2008 2:36 PM

Answers

  • Hi,

    I include some simple code that will reset the color of the parent to red.Simply add a treeview to your form named trvOne and edit the loading event as below.

    CODE START

    private void Testing_Load(object sender, EventArgs e)
    {
    LoadTreeViewNodes();
    trvOne.NodeMouseClick +=
    new TreeNodeMouseClickEventHandler(trvOne_NodeMouseClick);
    }

    void trvOne_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
    DeselectAll();
    if (e.Node.Parent != null)
    {
    //CC - Set the parents backcolor thus place your selection code here
    e.Node.Parent.BackColor = Color.Red;
    }
    }

    private void DeselectAll()
    {
    //Reset all the other nodes to have default back color
    foreach (TreeNode trN in this.trvOne.Nodes)
    {
    trN.BackColor =
    Color.White;
    }
    }

    private void LoadTreeViewNodes()
    {
    //CC - create the nodes with children
    for (int ParentCounter = 0; ParentCounter < 5; ParentCounter++)
    {

    TreeNode
    newNode = new TreeNode();
    newNode.Text =
    "Parent_" + ParentCounter.ToString();
    trvOne.Nodes.Add(
    "Parent_" + ParentCounter.ToString(), "Parent_" + ParentCounter.ToString());

    for(int childCounter = 0;childCounter < 5;childCounter++)
    {
    trvOne.Nodes[
    "Parent_" + ParentCounter.ToString()].Nodes.Add(ParentCounter.ToString() + childCounter.ToString() + " Child");}
    }
    }


    CODE END
    Hope this works.

    Monday, October 13, 2008 5:19 PM

All replies

  • This sample shows you how to do most of the basics with TreeView controls.  Searching, adding, deleting, etc.

    http://www.c-sharpcorner.com/UploadFile/scottlysle/TreeviewBasics04152007195731PM/TreeviewBasics.aspx

    Rudedog   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    Monday, October 13, 2008 4:51 PM
    Moderator
  • I am not sure that you can select more than one node at a time in TreeView Control.
    You can achieve by changing the backcolor property of the parent.
    Monday, October 13, 2008 4:57 PM
  • Cool Poison said:

    I am not sure that you can select more than one node at a time in TreeView Control.
    You can achieve by changing the backcolor property of the parent.



    The OP can "select" as many as they wish be changing the backcolor of the node, not the parent.  Selecting and highlighting nodes is covered in the link I provided.  The exact logic behind which nodes to "select" or "deselect" is up to the OP.  What if the tree goes 3 levels deep?  The sample does not cover that exact scenario, but it does show how to find the Parent node of a child node.

    Rudedog   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    Monday, October 13, 2008 5:02 PM
    Moderator
  • Hi,

    I include some simple code that will reset the color of the parent to red.Simply add a treeview to your form named trvOne and edit the loading event as below.

    CODE START

    private void Testing_Load(object sender, EventArgs e)
    {
    LoadTreeViewNodes();
    trvOne.NodeMouseClick +=
    new TreeNodeMouseClickEventHandler(trvOne_NodeMouseClick);
    }

    void trvOne_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
    DeselectAll();
    if (e.Node.Parent != null)
    {
    //CC - Set the parents backcolor thus place your selection code here
    e.Node.Parent.BackColor = Color.Red;
    }
    }

    private void DeselectAll()
    {
    //Reset all the other nodes to have default back color
    foreach (TreeNode trN in this.trvOne.Nodes)
    {
    trN.BackColor =
    Color.White;
    }
    }

    private void LoadTreeViewNodes()
    {
    //CC - create the nodes with children
    for (int ParentCounter = 0; ParentCounter < 5; ParentCounter++)
    {

    TreeNode
    newNode = new TreeNode();
    newNode.Text =
    "Parent_" + ParentCounter.ToString();
    trvOne.Nodes.Add(
    "Parent_" + ParentCounter.ToString(), "Parent_" + ParentCounter.ToString());

    for(int childCounter = 0;childCounter < 5;childCounter++)
    {
    trvOne.Nodes[
    "Parent_" + ParentCounter.ToString()].Nodes.Add(ParentCounter.ToString() + childCounter.ToString() + " Child");}
    }
    }


    CODE END
    Hope this works.

    Monday, October 13, 2008 5:19 PM