Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable or grey out a node in the TreeNode Editor

Tags:

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 (all nodes). I only want a few of the nodes disabled/hidden.

C# using Visual Studio 2005 TreeNode Editor.

like image 740
mr-euro Avatar asked Sep 22 '09 20:09

mr-euro


People also ask

How to disable a TreeNode in c#?

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).

What is TreeView node?

The Nodes property holds a collection of TreeNode objects, each of which has a Nodes property that can contain its own TreeNodeCollection. This nesting of tree nodes can make it difficult to navigate a tree structure, but the FullPath property makes it easier to determine your location within the tree structure.

How do you edit TreeView?

The TreeView allows you to edit nodes by setting the allowEditing property to true. To directly edit the nodes in place, double click the TreeView node or select the node and press F2 key. When editing is completed by focus out or by pressing the Enter key, the modified node's text saves automatically.


2 Answers

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); } 
like image 90
Fredrik Mörk Avatar answered Oct 05 '22 20:10

Fredrik Mörk


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;     } 
like image 23
Itataki Avatar answered Oct 05 '22 20:10

Itataki