Using just few lines of code its possible to render anything in AdvTree left margin and this article will illustrate how to add line numbers.

To add margin to the control set advTree.BackgroundStyle.PaddingLeft = 24 property. In this example it will be set to 24 pixels. Handle advTree.Render and advTree.GetRenderer().RenderNodeBackground events to render left margin background and line numbers. You can place this code in form load event:

advTree1.Render += new AdvTreeRenderEventHandler(AdvTreeRender);
advTree1.GetRenderer().RenderNodeBackground += new NodeRendererEventHandler(TreeRenderNodeBackground);

 

void TreeRenderNodeBackground(object sender, DevComponents.AdvTree.Display.NodeRendererEventArgs e)
{
    Graphics g = e.Graphics;
    Node n = e.Node;
    Rectangle r = e.NodeBounds;
    Rectangle numberBounds = new Rectangle(4, r.Y, advTree1.BackgroundStyle.PaddingLeft - 4, r.Height);
 
    Region oldClip = g.Clip;
 
    int i = advTree1.GetNodeFlatIndex(n);
    Rectangle lineNumberBounds = advTree1.ClientRectangle;
    lineNumberBounds.X += 1; // Border
    lineNumberBounds.Width = advTree1.BackgroundStyle.PaddingLeft - 1;
    lineNumberBounds.Y = advTree1.Columns.Bounds.Bottom;
    lineNumberBounds.Height = lineNumberBounds.Height - advTree1.Columns.Bounds.Bottom;
    if (advTree1.HScrollBar != null && advTree1.HScrollBar.Visible)
        lineNumberBounds.Height -= advTree1.HScrollBar.Height;
    g.SetClip(lineNumberBounds);
    g.SetClip(numberBounds, System.Drawing.Drawing2D.CombineMode.Intersect);
    g.DrawString(i.ToString(), advTree1.Font, Brushes.Red, numberBounds.Location);
 
    g.Clip = oldClip;
}
 
void AdvTreeRender(object sender, AdvTreeRenderEventArgs e)
{
    if (e.RenderType == eAdvTreeRenderType.Background)
    {
        AdvTree tree = (AdvTree)sender;
        Graphics g = e.Graphics;
        Region oldClip = g.Clip;
        g.ResetClip();
        g.FillRectangle(Brushes.WhiteSmoke, new Rectangle(0, 0, 24, tree.Height));
        g.Clip = oldClip;
    }
}

Related posts:

  1. How to enumerate through all nodes in AdvTree control
  2. How to right-align the cell text in AdvTree control
  3. AdvTree Drag & Drop Events And Options
  4. How to accept external drag & drop on AdvTree control
  5. How to Make DotNetBar for WinForms Floating Docking Windows Translucent