Checkbox .NET TreeView Control

The TreeView control has a checkboxes property, but it sets a checkbox for each node. How to check the box only for the nodes I want?

+3
source share
2 answers

Use StateImageList and TreeNode.StateImageIndex for such purposes. You also need to subscribe to the MouseDown event and change the scan status (status image) when the user clicks on the status image. Using this approach, you can also emulate flags of three states.

In fact, the internal implementation of TreeView uses virtually the same method, but it is hidden from you.

ImageList 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;
}
+3

TreeView, .

TreeViewAdv

0

Source: https://habr.com/ru/post/1714647/


All Articles