tags:

views:

581

answers:

3

The TreeView control has the checkboxes property, but it puts a checkbox on every node. How do I put a checkbox on just the nodes I want?

A: 

I gave up on TreeView because it is limited and buggy.

You should be able to get this behavior out of the box using the open source TreeViewAdv

Eric J.
What bugs did you come across in TreeView?
Neil N
Actually native TreeView is very powerful, but you however need to have many checks, because TreeView evolutes and gets new features with every new windows release. .Net tree view wrapper does not use all TreeView potential, and for many advanced features you need to subclass.
arbiter
It's been a while but one bug I recall is events not firing under some circumstances.
Eric J.
+3  A: 

Use StateImageList and TreeNode.StateImageIndex for such purposes. You also need to subscribe to MouseDown event and change check state (state image) when user clicks on state image. By using this approach you can also emulate three-state check boxes for example.

Actually internal TreeView implementation uses actually the same methodique but this is hidden from you.

Method for creating image for ImageList based on CheckBoxState:

private Image CreateCheckBoxGlyph(CheckBoxState state)
{
    Bitmap Result = new Bitmap(imlCheck.ImageSize.Width, imlCheck.ImageSize.Height);
    using (Graphics g = Graphics.FromImage(Result))
    {
        Size GlyphSize = CheckBoxRenderer.GetGlyphSize(g, state);
        CheckBoxRenderer.DrawCheckBox(g,
          new Point((Result.Width - GlyphSize.Width) / 2, (Result.Height - GlyphSize.Height) / 2), state);
    }
    return Result;
}
arbiter
A: 

You can try out the IntegralUI TreeView. It's a 3-rd party control and is not free. However, it allows you to determine which nodes can have check boxes, simply by setting teh CheckBoxVisible property for specified node to True or False.

Lokey