今回は置換機能を追加します。以前作成したSyncRichTextBoxクラスを使用します。
SyncRichTextBox.ReplaceStrings(string oldString, string newString, int start)
これをつかってみましょう。
あまりできのいいプログラミングとはいえませんが(関数長すぎ)、置換の箇所が多いときのためにプログレスバーを表示させるようにしています。
まず最初にダイアログを表示させて、置換は現在選択されているノードだけなのか、その配下のノードも含むのか、それともファイル全体なのかを選択させます。
そのあと置換の対象を検索させて逐次置換、一括置換かを選択するダイアログを表示します。一括置換が選択されたらダイアログは表示させる必要はありません。一括置換の場合は進行状況がわかるようにプログレスバーを表示させます。
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
public partial class Form1 : Form { ReplaceForm3 form3 = null; private void ReplaceToolStripMenuItem1_Click(object sender, EventArgs e) { SaveToMoromy(); ReplaceForm form = new ReplaceForm(this); var dr = form.ShowDialog(); if (dr == DialogResult.OK) { string oldString = form.textBox1.Text; string newString = form.textBox2.Text; List<TreeNodeEx> treeNodeExs = null; if (form.radioButtonOnly.Checked) { treeNodeExs = new List<TreeNodeEx>(); treeNodeExs.Add(syncTreeViewEx1.SelectedNode); } if (form.radioButtonAll.Checked) treeNodeExs = syncTreeViewEx1.GetTreeNodeExs(null); if (form.radioButtonChildren.Checked) treeNodeExs = syncTreeViewEx1.GetTreeNodeExs(syncTreeViewEx1.SelectedNode); treeNodeExs = treeNodeExs.Where(x => (GetData(x).RichData.Text != null && GetData(x).RichData.Text.IndexOf(oldString) != -1)).ToList(); bool isNonStop = false; SyncRichTextBox r0 = new SyncRichTextBox(); r0.Data = new RichData(); r0.ReplacingString += R0_ReplacingString; int i = 0; int allCount = treeNodeExs.Count; Point pt = new Point(100,100); foreach (TreeNodeEx nodeEx in treeNodeExs) { i++; if (isNonStop) { r0.Data = GetData(nodeEx).RichData; if (form3 != null && !form3.IsDisposed) { form3.progressBar1.Value++; string text1 = GetData(nodeEx).RichData.Text; System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(oldString); var rets1 = reg1.Matches(text1); int allCount1 = rets1.Count; form3.progressBar2.Value = 0; form3.progressBar2.Maximum = allCount1+1; } r0.ReplaceStrings(oldString, newString, 0); r0.SaveToMemory(); continue; } syncTreeViewEx1.SelectedNode = nodeEx; syncRichTextBoxEx1.SelectionStart = 0; bool isCancel = false; int i2 = 0; string text = GetData(nodeEx).RichData.Text; System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(oldString); var rets = reg.Matches(text); int allCount2 = rets.Count; while (true) { i2++; int charIndex = syncRichTextBoxEx1.SearchStringFromCurent(oldString); if (charIndex == -1) break; if (charIndex != -1) { ReplaceForm2 form2 = new ReplaceForm2(); form2.Location = pt; form2.label1.Text = String.Format("対象ページ{0}中 {1}ページ目 {2}/{3}", allCount, i, i2, allCount2); var dr1 = form2.ShowDialog(); pt = form2.Location; if (dr1 == DialogResult.Cancel) { isCancel = true; break; } if (form2.Action == ReplaceForm2.Actions.Once) { syncRichTextBoxEx1.SelectionLength = 0; syncRichTextBoxEx1.ReplaceOneString(oldString, newString, syncRichTextBoxEx1.SelectionStart); } else if (form2.Action == ReplaceForm2.Actions.Skip) { syncRichTextBoxEx1.SelectionStart = charIndex + oldString.Length; syncRichTextBoxEx1.SelectionLength = 0; } else if (form2.Action == ReplaceForm2.Actions.All) { form3 = new ReplaceForm3(); Task.Run(()=> { form3.progressBar2.Maximum = allCount2 - i2 + 1; form3.progressBar2.Minimum = 0; form3.ShowDialog(); }); syncRichTextBoxEx1.SelectionLength = 0; syncRichTextBoxEx1.ReplaceStrings(oldString, newString, syncRichTextBoxEx1.SelectionStart); form3.Dispose(); break; } else if (form2.Action == ReplaceForm2.Actions.Alls) { form3 = new ReplaceForm3(); Task.Run(() => { form3.progressBar1.Maximum = allCount - i + 1; form3.progressBar1.Minimum = 0; form3.progressBar2.Maximum = allCount2 - i2 + 1; form3.progressBar2.Minimum = 0; form3.ShowDialog(); }); syncRichTextBoxEx1.SelectionLength = 0; syncRichTextBoxEx1.ReplaceStrings(oldString, newString, syncRichTextBoxEx1.SelectionStart); isNonStop = true; break; } else if (form2.Action == ReplaceForm2.Actions.Cancel) { isCancel = true; break; } } } if (isCancel) break; } r0.Dispose(); if (form3 != null && !form3.IsDisposed) form3.Dispose(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public partial class Form1 : Form { public Form1() { syncRichTextBoxEx1.ReplacingString += syncRichTextBoxEx1_ReplacingString } private void R0_ReplacingString(SyncRichTextBox sender, ReplaceStringsArgs e) { if (form3 != null && !form3.IsDisposed) { form3.progressBar2.Value++; } } private void syncRichTextBoxEx1_ReplacingString(SyncRichTextBox sender, ReplaceStringsArgs e) { if (form3 != null && !form3.IsDisposed) { form3.progressBar2.Value++; } } } |