tags:

views:

23

answers:

2

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?

A: 

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;
}
Carra
I don't think this is a very good approach. Why depend on the color?
Slavo
Well, i guess this an approach that i would follow. Indeed a good suggestion! thanks Carra. Salvo, i would not depend on the color ... would be checking another variable! Thanks for the workaround.
Maneesh
It's just the general idea. You can use the node.Tag or a keep a list with disabled nodes.
Carra
+1  A: 

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;
    }
}
Oliver
Yes, BeforeCheck. The logic probably doesn't need Tag.
Hans Passant
Indeed doesn't the logic described above need the Tag. But to have it simpler (faster) at each BeforeCheck event i would determine if a node is checkable at creation time of the node and then just check the result at BeforeCheck (if that wouldn't be possible think about another cache solution)
Oliver