前回は検索結果を表示する機能を追加しました。
ところでノードが削除された場合、検索結果も変更しなければならない場合があります。今回はそれについて考えます。
ノードが削除されることでいっしょに削除されてしまう子ノードは、SyncTreeView.GetTreeNodeExsを使えばわかります。そこで検索結果のなかにSyncTreeView.GetTreeNodeExs(deleteNodes)があるかどうかを調べればよいということになります。
以下は検索結果のなかに削除されるアイテムがあれば、リストボックスのなかから削除するメソッドです。リストボックスは削除後でも同じアイテムが選択されるようになっています。また削除に伴って選択されているアイテムが変化しても影響がでないように、フィールド変数 SearchResultForm._isIgnoreSelectChangedで制御しています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public partial class Form1 : Form { void DeteleNodes(TreeNodeEx targetNode) { if (targetNode == null) return; SaveToMoromy(); if (TrashNode == targetNode) TrashClear(); else DeteleNodes0(targetNode); SetPageNumbers(); } } |
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 |
public partial class Form1 : Form { void DeleteNodeInSearchResult(List<SearchResultForm> searchResultForms, List<TreeNodeEx> deletingNode) { foreach (var form in searchResultForms) { int i = form.listBox1.SelectedIndex; TreeNodeEx nodeEx = null; if(i!=-1) nodeEx = form._treeNodeExes[i]; foreach (TreeNodeEx nodeEx1 in deletingNode) { TreeNodeEx treeNode = form._treeNodeExes.FirstOrDefault(x => x == nodeEx1); if (treeNode != null) form._treeNodeExes.Remove(treeNode); } form._isIgnoreSelectChanged = true; form.listBox1.Items.Clear(); foreach (TreeNodeEx nodeEx1 in form._treeNodeExes) form.listBox1.Items.Add(nodeEx1.Text); if (nodeEx != null) { i = form._treeNodeExes.IndexOf(nodeEx); } if (i != -1) { form.listBox1.SelectedIndex = i; form._prevSelect = i; } else { form.listBox1.SelectedItem = null; form._prevSelect = -1; form.syncRichTextBoxEx1.Data = new RichData(); } form._isIgnoreSelectChanged = false; } } } |
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 |
public partial class Form1 : Form { void DeteleNodes0(TreeNodeEx targetNode) { var deleteNode = targetNode; TreeNodeEx nextSelectNode = null; if (deleteNode.NextNode != null) nextSelectNode = deleteNode.NextNode; else if (deleteNode.PrevNode != null) nextSelectNode = deleteNode.PrevNode; else if (deleteNode.ParentNode != null) nextSelectNode = deleteNode.ParentNode; else return; var deletingNode = syncTreeViewEx1.GetTreeNodeExs(targetNode); List<SyncTreeViewEx> syncTreeViewExs = new List<SyncTreeViewEx>(); List<SearchResultForm> searchResultForms = new List<SearchResultForm>(); foreach (SyncTreeViewEx views in syncTreeViewEx1.SyncTreeViews) { if (views.SelectedNode == null) continue; if (deletingNode.Any(x => x == views.SelectedNode)) syncTreeViewExs.Add(views); } foreach (SearchResultForm form in SearchResultForms) { int i = form.listBox1.SelectedIndex; int i = form.listBox1.SelectedIndex; if (i == -1) continue; if (deletingNode.Any(x => x == form._treeNodeExes[i])) searchResultForms.Add(form); } if (syncTreeViewExs.Count > 1 || searchResultForms.Count > 0) { var dr = MessageBox.Show( "別のTreeViewで選択中のノードが削除されようとしています。削除しますか?", "確認", MessageBoxButtons.YesNo); if (dr == DialogResult.No) return; } foreach (var view in syncTreeViewExs) view.SelectedNode = nextSelectNode; if (syncTreeViewEx1.IsInTrash(targetNode)) { syncTreeViewEx1.DeleteNode(targetNode); DeleteNodeInSearchResult(searchResultForms, deletingNode); } else { syncTreeViewEx1.MoveToLastChild(targetNode, TrashNode); } } } |
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 |
public partial class Form1 : Form { void TrashClear() { TreeNodeEx nextSelectNode = TrashNode; var deletingNodes = syncTreeViewEx1.GetTreeNodeExs(TrashNode); deletingNodes.Remove(TrashNode); List<SyncTreeViewEx> syncTreeViewExs = new List<SyncTreeViewEx>(); foreach (SyncTreeViewEx views in syncTreeViewEx1.SyncTreeViews) { if (views.SelectedNode == null) continue; if (deletingNodes.Any(x => x == views.SelectedNode)) syncTreeViewExs.Add(views); } List<SearchResultForm> searchResultForms = new List<SearchResultForm>(); foreach (SearchResultForm form in SearchResultForms) { int i = form.listBox1.SelectedIndex; if (i == -1) continue; if (deletingNodes.Any(x => x == form._treeNodeExes[i])) searchResultForms.Add(form); } if (syncTreeViewExs.Count > 0 || searchResultForms.Count > 0) { var dr = MessageBox.Show("別のTreeViewで選択中のノードが削除されようとしています。削除しますか?", "", MessageBoxButtons.YesNo); if (dr == DialogResult.No) return; } foreach (var view in syncTreeViewExs) view.SelectedNode = nextSelectNode; DeleteNodeInSearchResult(SearchResultForms, deletingNodes); while (TrashNode.FirstNode != null) syncTreeViewEx1.DeleteNode(TrashNode.FirstNode); } } |
次にSearchResultFormクラスを示します。
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 |
public partial class SearchResultForm : Form { public SearchResultForm() { InitializeComponent(); } public int _prevSelect = -1; public List<TreeNodeEx> _treeNodeExes = null; public bool _isIgnoreSelectChanged = false; private void SearchResultForm_Load(object sender, EventArgs e) { foreach (TreeNodeEx nodeEx in _treeNodeExes) { listBox1.Items.Add(nodeEx.Text); } } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { if (_isIgnoreSelectChanged) return; if (_prevSelect != -1) { Data data = (Data)_treeNodeExes[_prevSelect].Data; data.RichData = syncRichTextBoxEx1.Data; } _prevSelect = listBox1.SelectedIndex; if (_prevSelect != -1) { Data data1 = (Data)_treeNodeExes[_prevSelect].Data; syncRichTextBoxEx1.Data = data1.RichData; } } } |
Form1.SaveToMoromyメソッドも少し変更する必要があります。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public partial class Form1 : Form { public void SaveToMoromy() { if(syncRichTextBoxEx1.Modified) syncRichTextBoxEx1.SaveToMemory(); if (syncRichTextBoxEx2.Modified) syncRichTextBoxEx2.SaveToMemory(); foreach (SearchResultForm f in SearchResultForms) f.syncRichTextBoxEx1.SaveToMemory(); } } |