NodePaintBox
If you need to display data that has no built-in node control support, you can display it manually using the NodePaintBox node control.
Using the NodePaintBox node control involves these steps:
- Measure the node control size inside a node using the MeasureNodeControl treeview event.
- Draw data using the PaintNodeControl treeview event.
Example
// Create the node control.
NodePaintBox nc = new NodePaintBox();
nc.AttachTo(tree);
// Subscribe to the event to define the node control's size and content.
tree.MeasureNodeControl += tree_MeasureNodeControl;
tree.PaintNodeControl += tree_PaintNodeControl;
// Add nodes, where our data will be displayed.
Node n = new Node("1");
n.AttachTo(tree);
n = new Node("2");
n.AttachTo(tree);
private void tree_MeasureNodeControl(FlexibleTreeView treeview, MeasureObjectEventArgs args)
{
// Measure the node control size according to the specified node
switch(args.Node.Text)
{
case "1":
args.Size = new Size(20, 20);
break;
case "2":
args.Size = new Size(30, 30);
break;
}
}
// Paint the node control content as a red rectangle.
void tree_PaintNodeControl(FlexibleTreeView treeview, NodeControlDrawEventArgs args)
{
using (Brush br = new SolidBrush(Color.Red))
{
args.Context.Graphics.FillRectangle(br, args.Context.Bounds);
}
}
' Create the node control.
Dim nc As New NodePaintBox()
nc.AttachTo(tree)
' Subscribe to the event to define the node control's size and content.
AddHandler tree.MeasureNodeControl, AddressOf tree_MeasureNodeControl
AddHandler tree.PaintNodeControl, AddressOf tree_PaintNodeControl
' Add nodes, where our data will be displayed.
Dim n As New Node("1")
n.AttachTo(tree)
n = New Node("2")
n.AttachTo(tree)
Private Sub tree_MeasureNodeControl(treeview As FlexibleTreeView, args As MeasureObjectEventArgs)
' Measure the node control size according to the specified node
Select Case args.Node.Text
Case "1"
args.Size = New Size(20, 20)
Case "2"
args.Size = New Size(30, 30)
End Select
End Sub
' Paint the node control content as a red rectangle.
Private Sub tree_PaintNodeControl(treeview As FlexibleTreeView, args As NodeControlDrawEventArgs)
Using br As Brush = New SolidBrush(Color.Red)
args.Context.Graphics.FillRectangle(br, args.Context.Bounds)
End Using
End Sub