Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make owner-drawn TreeNode class arrange items like in indented tree?
    text
    copied!<p>So there is a great open source <a href="http://blog.csharphelper.com/2010/11/23/handle-mouse-events-for-a-generic-treenode-class-in-c.aspx" rel="nofollow noreferrer">tutorial on creating TreeNode class</a>. It is great. But I wonder how to change its Arrange function to make it draw <a href="http://mbostock.github.com/protovis/ex/indent.html" rel="nofollow noreferrer">indented</a> tree?</p> <p>Here's the function:</p> <pre><code>// Arrange the node and its children in the allowed area. // Set xmin to indicate the right edge of our subtree. // Set ymin to indicate the bottom edge of our subtree. public void Arrange(Graphics gr, ref float xmin, ref float ymin) { // See how big this node is. SizeF my_size = Data.GetSize(gr, MyFont); // Recursively arrange our children, // allowing room for this node. float x = xmin; float biggest_ymin = ymin + my_size.Height; float subtree_ymin = ymin + my_size.Height + Voffset; foreach (TreeNode&lt;T&gt; child in Children) { // Arrange this child's subtree. float child_ymin = subtree_ymin; child.Arrange(gr, ref x, ref child_ymin); // See if this increases the biggest ymin value. if (biggest_ymin &lt; child_ymin) biggest_ymin = child_ymin; // Allow room before the next sibling. x += Hoffset; } // Remove the spacing after the last child. if (Children.Count &gt; 0) x -= Hoffset; // See if this node is wider than the subtree under it. float subtree_width = x - xmin; if (my_size.Width &gt; subtree_width) { // Center the subtree under this node. // Make the children rearrange themselves // moved to center their subtrees. x = xmin + (my_size.Width - subtree_width) / 2; foreach (TreeNode&lt;T&gt; child in Children) { // Arrange this child's subtree. child.Arrange(gr, ref x, ref subtree_ymin); // Allow room before the next sibling. x += Hoffset; } // The subtree's width is this node's width. subtree_width = my_size.Width; } // Set this node's center position. Center = new PointF( xmin + subtree_width / 2, ymin + my_size.Height / 2); // Increase xmin to allow room for // the subtree before returning. xmin += subtree_width; // Set the return value for ymin. ymin = biggest_ymin; } </code></pre> <p>How it looks now:</p> <p><img src="https://i.stack.imgur.com/p92JY.jpg" alt="enter image description here"></p> <p>How indented tree looks like ( image based on <a href="https://stackoverflow.com/questions/7860609/how-to-make-owner-drawn-treenode-class-arrange-items-like-in-indented-tree/7889792#7889792">DmitryG s grate answer</a>):</p> <p><img src="https://i.stack.imgur.com/cvr3V.jpg" alt="enter image description here"></p> <p>So.. How do I make it draw the graph in the indented form?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload