Disable or grey out a node in the TreeNode Editor

前端 未结 4 883
遥遥无期
遥遥无期 2021-01-03 21:12

How do I disable a specific node so the user can not select it. Hiding it for the user is also valid.

I tried the Visible property but that hides the entire tree (al

相关标签:
4条回答
  • 2021-01-03 21:26

    set disabled node by yourNode.SelectAction = TreeNodeSelectAction.None

    I think you need also disable expandable this node yourNode.PopulateOnDemand = false

    0 讨论(0)
  • 2021-01-03 21:37

    Two options:

    1. Add and remove the nodes on the fly.
    2. Owner draw and handle the clicks and send it to another node.
    0 讨论(0)
  • 2021-01-03 21:45

    The TreeNode itself does not have any Enabled property, so you will need to find some means of tracking that state. One way to do this is to create a new class that inherits TreeNode and that features an Enabled property. Another way is to maintain a list of disabled tree nodes.

    Once that is done, you can use the ForeColor property of the TreeNode to have it appear grayed out (for instance using the SystemColors.GrayText value).

    Finally you can use the BeforeSelect event to evaluate whether it's OK for the user to select a particular node, and use the Cancel property of the event args in that event to prevent selecting it if that node is disabled:

    private void TreeView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
    {
        e.Cancel = !NodeIsEnabled(e.Node);
    }
    
    0 讨论(0)
  • 2021-01-03 21:51

    I just found another way to handle the disabled treenodes. If you gray in the treenodes you don't want to use, you can ask for the color and not allow all grayed nodes.

        private void TreeView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if(SystemColors.GrayText==e.Node.ForeColor)
                e.Cancel = true;
        }
    
    0 讨论(0)
提交回复
热议问题