siva
siva

Reputation: 1265

Disable certain nodes of a tree control

Hello i have a treeView control with checkboxes:

checkbox LEVEL1

  checkbox Child1
  checkbox Child2

checkbox LEVEL2

  checkbox Child1

I shloul not allow checking and unchecking of Child2 of Level 1 and Child 1 of Level 2?

is that possible in a tree View control?

Upvotes: 0

Views: 2550

Answers (2)

Oliver
Oliver

Reputation: 45101

The problem is, that a TreeNode doesn't have a Enabled state nor any event you can ask. So to emulate the Enabled state you could use the Tag property and save a boolean value there when you create each node.

Then you add an event to the TreeView.BeforeCheck and implement in some kind of this:

void TreeView_BeforeCheck(object sender, TreeViewCancelEventArgs e)
{
    var IsReadOnly = e.Node.Tag as bool?;

    if (IsReadOnly != null)
    {
        e.Cancel = IsReadOnly.Value;
    }
}

Upvotes: 1

Carra
Carra

Reputation: 17964

It's not possible as far as I know. But you can emulate it yourself:

Change the node color to gray:

treeControl.Nodes[0].ForeColor = Color.Gray;

And catch the click event:

private void treeControl_AfterCheck(TreeControl tc,
                                            NodeEventArgs e)
{
  if(e.Node.ForeColor == Color.Gray)
    e.Node.Checked = !e.Node.Checked;
}

Upvotes: 0

Related Questions