1

I'm obliged to use VirtualStringTree component in C++ Builder (RAD Studio XE5). And I need to provide a possibility to negative-check elements of my tree.

For example, let's take a look at the node with no child. I would like to be able to check this node with "V" symbol (as usual) and with "X" symbol. And of course this node must be able to be unchecked. The "X" may be set with the second click at the node or right button click.

How is it possible to implement?

Built-in tri-state checkboxes, unfortunately, provide only an additional state for nodes, which have checked and unchecked children at the same time.

MYriad
  • 118
  • 7

1 Answers1

1

I have found such way.

  1. In the node's record structure must be added additional field showing its check state (it could have type char for example).

  2. Then choose node's CheckType = ctButton. In that case OnChecked event is triggered on click on this small button. Each click must increase node's check state in a ring (0->1->2->3->0->1...).

    if(checkState < 3)
        ++checkState;
    else
        checkState = 0;
    
  3. The final step is to redraw button with your image. I have used three images: empty square, "V" and "X". Images could be placed in ImageList and should have size 15x15. Then Define OnAfterCellPaint event like this:

    void __fastcall TSomeForm::TreeAfterCellPaint(TBaseVirtualTree *Sender,
      TCanvas *TargetCanvas, PVirtualNode Node, TColumnIndex Column,
      TRect &CellRect)
    {
        Record* record = (Record*)Tree->GetNodeData(Node);
        int offset = 22 + 18 * Tree->GetNodeLevel(Node);
        CheckBoxesImageList->Draw(TargetCanvas, CellRect.Left + offset, CellRect.Top + 1, record->CheckState);
    }
    

    The offset was found experimentally :)

MYriad
  • 118
  • 7