none
How to move the selected node to be deleted will move to the last node and the end node icon will be changed ? RRS feed

  • Question

  • How to move the selected node to be deleted will move to the last node and the end node icon will be changed?
    I have the following code:

    private void cmdDel_Click(object sender, EventArgs e)
            {
                TreeNode nodeToMove = treeView1.SelectedNode;
                TreeNodeCollection nodes = treeView1.Nodes;            
                TreeNode RecycleBinNode = clsTreeView.RecycleBinNode(nodes);
                try
                {
                    if (nodeToMove.Parent == null) 
                        treeView1.Nodes.Remove(nodeToMove);
                    else
                        nodeToMove.Parent.Nodes.Remove(nodeToMove);
    
                    treeView1.SelectedNode = RecycleBinNode;
                    treeView1.SelectedNode.Nodes.Add(nodeToMove);                                
                }
                catch (Exception ex)
                {
                    throw ex;
                }
    }

    When deleting the selected node, it will move to the last node (or the trash button). If the last node is ExpandAll, the last node icon will be changed. Is there any way to fix the icon that has not been changed when the last node has been extracted ? You see the attached image


    Tuesday, April 28, 2020 9:40 AM

All replies


  • Hi lamtriendong,
    >>TreeNode RecycleBinNode = treeView1.Nodes["Trash can"];
    First, I can't move the selected node to the last node via your code. So I change this statement to 

    TreeNode RecycleBinNode = treeView1.Nodes["Trash can"];

    Note:"Trash can" is last node (or the trash button).
    And then you can change the last node icon by check if it includes child node.
    Here is a code example you can refer to.

    private void Form1_Load(object sender, EventArgs e)
    {
        treeView1.ImageList = Form1.ImageList;
        treeView1.ImageKey = "child node";
        treeView1.SelectedImageKey = "child node";
        treeView1.Nodes[0].ImageKey = "home";
        treeView1.Nodes[0].SelectedImageKey = "home";
        treeView1.Nodes[1].ImageKey = "root node";
        treeView1.Nodes[1].SelectedImageKey = "root node";
        treeView1.Nodes[2].ImageKey = "root node";
        treeView1.Nodes[2].SelectedImageKey = "root node";
        treeView1.Nodes[3].ImageKey = "root node";
        treeView1.Nodes[3].SelectedImageKey = "root node";
        treeView1.Nodes[4].ImageKey = "end node";
        treeView1.Nodes[4].SelectedImageKey = "end node";
    }
    
    static ImageList _imageList;
    public static ImageList ImageList
    {
        get
        {
            if (_imageList == null)
            {
                _imageList = new ImageList();
                _imageList.Images.Add("home", Image.FromFile(@"C:\Users\Pictures\home.PNG"));
                _imageList.Images.Add("child node", Image.FromFile(@"C:\Users\Pictures\icon.PNG"));
                _imageList.Images.Add("root node", Image.FromFile(@"C:\Users\Pictures\file icon.PNG"));
                _imageList.Images.Add("end node", Image.FromFile(@"C:\Users\Pictures\trash.png"));
            }
            return _imageList;
        }
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
        TreeNode nodeToMove = treeView1.SelectedNode;
        TreeNodeCollection nodes = treeView1.Nodes;
        TreeNode RecycleBinNode = treeView1.Nodes["Trash can"];
        try
        {
            if (nodeToMove.Parent == null)
                treeView1.Nodes.Remove(nodeToMove);
            else
                nodeToMove.Parent.Nodes.Remove(nodeToMove);
    
            treeView1.SelectedNode = RecycleBinNode;
            treeView1.SelectedNode.Nodes.Add(nodeToMove);
        }
        catch (Exception ex)
        {
            throw ex;
    }
    // change the last node icon by check if it includes child node
        if (RecycleBinNode.FirstNode != null)
        {
            treeView1.Nodes["Trash can"].ImageKey = "root node";
            treeView1.Nodes["Trash can"].SelectedImageKey = "root node";
        }
    }

    Thr result:



    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 29, 2020 5:48 AM
  • So treeView when adding (or moving the selected node) node into the treeView Recycle Bin does not display the bin icon as shown in Figure N5, treeView does not work when extending nodes, right ?

    TreeView can do as this video clip ?

    https://www.mediafire.com/file/2rl051q1zj7aqcg/listview.swf/file

    Wednesday, April 29, 2020 8:25 AM
  • Hi lamtriendong,
    You can change the trash node icon under any conditions by using the following code:

    treeView1.Nodes["Trash can"].ImageKey = "root node";
    treeView1.Nodes["Trash can"].SelectedImageKey = "root node";

    In my code example, I change the trash node icon when moving selected node to the last node (or the trash node)
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 29, 2020 9:29 AM