public static void CenterNode(TreeNodeAdv node, int visibleLines) { // This function will only work properly if: // 1) All TreeNodes have a fixed vertical height // 2) The tree is fully collapsed except the ancestors // of this node. // 3) visibleLines is the actual number of nodes that can be // displayed in the window vertically. int paddingabove = (visibleLines - 1) / 2; int paddingbelow = visibleLines - 1 - paddingabove; TreeViewAdv tree = node.Tree; TreeNodeAdv viewtop = node; while (paddingabove > 0) { if (viewtop.PreviousNode != null) { viewtop = viewtop.PreviousNode; } else if (viewtop.Parent != null) { viewtop = viewtop.Parent; } else { break; } paddingabove--; } tree.EnsureVisible(viewtop); TreeNodeAdv viewbottom = node; node.Tree.EnsureVisible(node); while (paddingbelow > 0) { if (viewbottom.NextNode != null) { viewbottom = viewbottom.NextNode; } else if ((viewbottom.Parent != null) && (viewbottom.Parent.NextNode != null)) { viewbottom = viewbottom.Parent.NextNode; } else { break; } paddingbelow--; } tree.EnsureVisible(viewbottom); tree.EnsureVisible(node); }