/// <summary> /// Checks that moving down the selected tree node is possible. /// </summary> public static bool MoveDownIsEnabled(TreeNode selectedNode, TreeNodeBehavior moveBehavior) { if (selectedNode == null) { return(false); } else if (selectedNode.NextNode != null) { return(true); } else if (moveBehavior == TreeNodeBehavior.ThroughSimilarParents) { TreeNode parentNode = selectedNode.Parent; TreeNode parentNodeNext = parentNode?.NextNode; return(parentNode != null && parentNodeNext != null && (parentNode.Tag is ITreeNode && parentNodeNext.Tag is ITreeNode && parentNode.Tag.GetType() == parentNodeNext.Tag.GetType() || parentNode.Tag is TreeNodeTag tag1 && parentNodeNext.Tag is TreeNodeTag tag2 && tag1.NodeType == tag2.NodeType)); } else { return(false); } }
/// <summary> /// Checks that moving up the selected tree node is possible. /// </summary> public static bool MoveUpSelectedNodeIsEnabled(this TreeView treeView, TreeNodeBehavior moveBehavior) { TreeNode selectedNode = treeView.SelectedNode; if (selectedNode == null) { return(false); } else if (selectedNode.PrevNode != null) { return(true); } else if (moveBehavior == TreeNodeBehavior.ThroughSimilarParents) { TreeNode parentNode = selectedNode.Parent; TreeNode parentNodePrev = parentNode?.PrevNode; return(parentNode != null && parentNodePrev != null && (parentNode.Tag is ITreeNode && parentNodePrev.Tag is ITreeNode && parentNode.Tag.GetType() == parentNodePrev.Tag.GetType() || parentNode.Tag is TreeNodeTag tag1 && parentNodePrev.Tag is TreeNodeTag tag2 && tag1.NodeType == tag2.NodeType)); } else { return(false); } }
/// <summary> /// Moves down the selected tree node and the associated list item. /// </summary> /// <remarks>Use this method if the underlying list items implement ITreeNode.</remarks> public static void MoveDownSelectedNode(this TreeView treeView, TreeNodeBehavior moveBehavior) { TreeNode selectedNode = treeView.SelectedNode; if (GetRelatedObject(selectedNode) is not ITreeNode selectedObj || selectedObj.Parent == null) { return; } try { treeView.BeginUpdate(); TreeNodeCollection siblings = treeView.GetChildNodes(selectedNode.Parent); IList list = selectedObj.Parent.Children; int index = selectedNode.Index; int newIndex = index + 1; if (newIndex < siblings.Count) { siblings.RemoveAt(index); siblings.Insert(newIndex, selectedNode); list.RemoveAt(index); list.Insert(newIndex, selectedObj); treeView.SelectedNode = selectedNode; } else if (moveBehavior == TreeNodeBehavior.ThroughSimilarParents) { TreeNode parentNode = selectedNode.Parent; TreeNode parentNodeNext = parentNode?.NextNode; if (parentNode?.Tag is ITreeNode && parentNodeNext?.Tag is ITreeNode nextParentObj && parentNode.Tag.GetType() == parentNodeNext.Tag.GetType()) { // the node being moved gets a new parent siblings.RemoveAt(index); parentNodeNext.Nodes.Insert(0, selectedNode); list.RemoveAt(index); nextParentObj.Children.Insert(0, selectedObj); selectedObj.Parent = nextParentObj; treeView.SelectedNode = selectedNode; } } } finally { treeView.EndUpdate(); } }
/// <summary> /// Checks that moving down the selected tree node is possible. /// </summary> public static bool MoveDownSelectedNodeIsEnabled(this TreeView treeView, TreeNodeBehavior moveBehavior) { return(MoveDownIsEnabled(treeView.SelectedNode, moveBehavior)); }