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?
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
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;
}
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.