今回はノードの位置を変えます。
次(下)の位置へ変更
前(上)の位置へ変更
階層を上げる
階層を下げる
画像は階層を下げたときの実行結果です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
public partial class Form1 : Form { private void MoveToPrevToolStripMenuItem_Click(object sender, EventArgs e) { ignoreSelectedNodeChange = true; TreeNodeEx node = syncTreeViewEx1.SelectedNode; syncTreeViewEx1.MoveToPrev(node); ignoreSelectedNodeChange = false; } private void MoveToNextToolStripMenuItem_Click(object sender, EventArgs e) { ignoreSelectedNodeChange = true; TreeNodeEx node = syncTreeViewEx1.SelectedNode; syncTreeViewEx1.MoveToNext(node); ignoreSelectedNodeChange = false; } private void LevelUpToolStripMenuItem_Click(object sender, EventArgs e) { ignoreSelectedNodeChange = true; TreeNodeEx node = syncTreeViewEx1.SelectedNode; syncTreeViewEx1.LevelUp(node); ignoreSelectedNodeChange = false; } private void LevelDownToolStripMenuItem_Click(object sender, EventArgs e) { ignoreSelectedNodeChange = true; TreeNodeEx node = syncTreeViewEx1.SelectedNode; syncTreeViewEx1.LevelDown(node); ignoreSelectedNodeChange = false; } bool ignoreSelectedNodeChange = false; private void syncTreeViewEx1_BeforeSelect(SyncTreeView sender, BeforeSelectArgs e) { if (ignoreSelectedNodeChange) return; // あとは以前と同じ } // 以下も同様に private void syncTreeViewEx1_AfterSelect(SyncTreeView sender, AfterSelectArgs e) private void syncTreeViewEx2_BeforeSelect(SyncTreeView sender, BeforeSelectArgs e) private void syncTreeViewEx2_AfterSelect(SyncTreeView sender, AfterSelectArgs e) } |
ノードの位置を変更することで選択ノードが変化してしまうことがあります。フィールド変数 ignoreSelectedNodeChangeによってそのときはsyncTreeViewEx1_BeforeSelectなどでRichTextBoxの内容が変わらないようにしています。
SyncTreeView.MoveToPrev
SyncTreeView.MoveToNext
SyncTreeView.LevelUp
SyncTreeView.LevelDown
これらの内容は以下のようになっています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
public partial class SyncTreeView : UserControl { public void MoveToNext(TreeNodeEx sourceNode) { if (!CanMoveToNext(sourceNode)) return; foreach (TreeViewEx view in treeViewEx1.group.TreeViews) { TreeNode[] sourceNode0 = view.Nodes.Find(sourceNode.Key, true); if (sourceNode0.Length == 1) { view.MoveToNext(sourceNode0[0]); } } } public void MoveToPrev(TreeNodeEx sourceNode) { if (!CanMoveToPrev(sourceNode)) return; foreach (TreeViewEx view in treeViewEx1.group.TreeViews) { TreeNode[] sourceNode0 = view.Nodes.Find(sourceNode.Key, true); if (sourceNode0.Length == 1) { view.MoveToPrev(sourceNode0[0]); } } } public void LevelUp(TreeNodeEx sourceNode) { if (!CanLevelUp(sourceNode)) return; foreach (TreeViewEx view in treeViewEx1.group.TreeViews) { TreeNode[] sourceNode0 = view.Nodes.Find(sourceNode.Key, true); if (sourceNode0.Length == 1) { view.LevelUp(sourceNode0[0]); } } } public void LevelDown(TreeNodeEx sourceNode) { if (!CanLevelDown(sourceNode)) return; foreach (TreeViewEx view in treeViewEx1.group.TreeViews) { TreeNode[] sourceNode0 = view.Nodes.Find(sourceNode.Key, true); if (sourceNode0.Length == 1) { view.LevelDown(sourceNode0[0]); } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
public partial class SyncTreeView : UserControl { public bool CanMoveToNext(TreeNodeEx sourceNode) { if (sourceNode == null) return false; TreeNode[] sourceNodes = treeViewEx1.Nodes.Find(sourceNode.Key, true); if (sourceNodes.Length != 1) return false; return treeViewEx1.CanMoveToNext(sourceNodes[0]); } public bool CanMoveToPrev(TreeNodeEx sourceNode) { if (sourceNode == null) return false; TreeNode[] sourceNodes = treeViewEx1.Nodes.Find(sourceNode.Key, true); if (sourceNodes.Length != 1) return false; return treeViewEx1.CanMoveToPrev(sourceNodes[0]); } public bool CanLevelUp(TreeNodeEx sourceNode) { if (sourceNode == null) return false; TreeNode[] sourceNodes = treeViewEx1.Nodes.Find(sourceNode.Key, true); if (sourceNodes.Length != 1) return false; return treeViewEx1.CanLevelUp(sourceNodes[0]); } public bool CanLevelDown(TreeNodeEx sourceNode) { if (sourceNode == null) return false; TreeNode[] sourceNodes = treeViewEx1.Nodes.Find(sourceNode.Key, true); if (sourceNodes.Length != 1) return false; return treeViewEx1.CanLevelDown(sourceNodes[0]); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
internal class TreeViewEx : TreeView { public void MoveToNext(TreeNode sourceNode) { if (CanMoveToNext(sourceNode)) { MoveToNext(sourceNode, sourceNode.NextNode); } } public void MoveToPrev(TreeNode sourceNode) { if (CanMoveToPrev(sourceNode)) { MoveToPrev(sourceNode, sourceNode.PrevNode); } } public void LevelUp(TreeNode sourceNode) { if (CanLevelUp(sourceNode)) { MoveToNext(sourceNode, sourceNode.Parent); } } public void LevelDown(TreeNode sourceNode) { if (CanLevelDown(sourceNode)) { MoveToLastChild(sourceNode, sourceNode.PrevNode); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
internal class TreeViewEx : TreeView { public bool CanMoveToNext(TreeNode sourceNode) { if (sourceNode.NextNode != null) return true; else return false; } public bool CanMoveToPrev(TreeNode sourceNode) { if (sourceNode.PrevNode != null) return true; else return false; } public bool CanLevelUp(TreeNode sourceNode) { if (sourceNode.Parent != null) return true; else return false; } public bool CanLevelDown(TreeNode sourceNode) { if (sourceNode.PrevNode != null) return true; else return false; } } |
TreeNodeを移動させる前と後は同じノードが選択されるようにしています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
internal class TreeViewEx : TreeView { public void MoveToLastChild(TreeNode sourceNode, TreeNode targetNode) { if (CanMoveToLastChild(sourceNode, targetNode)) { TreeNode curNode = SelectedNode; sourceNode.Remove(); targetNode.Nodes.Add(sourceNode); SelectedNode = curNode; } } public void MoveToFirstChild(TreeNode sourceNode, TreeNode targetNode) { if (CanMove(sourceNode, targetNode)) { TreeNode curNode = SelectedNode; sourceNode.Remove(); targetNode.Nodes.Insert(0, sourceNode); SelectedNode = curNode; } } public void MoveToNext(TreeNode sourceNode, TreeNode targetNode) { if (CanMoveToNext(sourceNode, targetNode)) { TreeNode curNode = SelectedNode; sourceNode.Remove(); int index = targetNode.Index; if (targetNode.Parent != null) targetNode.Parent.Nodes.Insert(index + 1, sourceNode); else Nodes.Insert(index + 1, sourceNode); SelectedNode = curNode; } } public void MoveToPrev(TreeNode sourceNode, TreeNode targetNode) { if (CanMoveToPrev(sourceNode, targetNode)) { TreeNode curNode = SelectedNode; sourceNode.Remove(); int index = targetNode.Index; if (targetNode.Parent != null) targetNode.Parent.Nodes.Insert(index, sourceNode); else Nodes.Insert(index, sourceNode); SelectedNode = curNode; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
internal class TreeViewEx : TreeView { public bool CanMoveToLastChild(TreeNode sourceNode, TreeNode targetNode) { if (!CanMove(sourceNode, targetNode)) return false; if (targetNode.LastNode == sourceNode) return false; return true; } public bool CanMoveToFirstChild(TreeNode sourceNode, TreeNode targetNode) { if (!CanMove(sourceNode, targetNode)) return false; if (targetNode.FirstNode == sourceNode) return false; return true; } public bool CanMoveToNext(TreeNode sourceNode, TreeNode targetNode) { if (!CanMove(sourceNode, targetNode)) return false; if (targetNode.NextNode == sourceNode) return false; return true; } public bool CanMoveToPrev(TreeNode sourceNode, TreeNode targetNode) { if (!CanMove(sourceNode, targetNode)) return false; if (targetNode.PrevNode == sourceNode) return false; return true; } } |