TextBoxのアンドゥは1回しかできません。Ctrl+Zキーを押すとアンドゥされた状態とそのままの状態が繰り返されるだけです。2回以上のアンドゥができる(もちろんリドゥもできる)TextBoxを自作しました。
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
using System.Collections.Generic; using System.Windows.Forms; namespace NamespaceTextBoxEx { class TextBoxEx : TextBox { List<UndoBuf> undoBufs = new List<UndoBuf>(); List<UndoBuf> redoBufs = new List<UndoBuf>(); protected override void OnKeyPress(KeyPressEventArgs e) { e.Handled = true; int i = (int)e.KeyChar; if(i < 0x20 || i > 0x80) return; var str = e.KeyChar.ToString(); SelectedText = str; base.OnKeyPress(e); } protected override void OnKeyDown(KeyEventArgs e) { if(e.KeyCode == Keys.Back) { e.Handled = true; Back(); return; } if(e.KeyCode == Keys.Delete) { e.Handled = true; Delete(); return; } if(e.KeyCode == Keys.Enter) { e.Handled = true; BreakLine(); return; } if(e.KeyCode == Keys.X) { e.Handled = true; Cut(); return; } if(e.KeyCode == Keys.C) { e.Handled = true; Copy(); return; } if((e.KeyCode == Keys.V && e.Modifiers == Keys.Control) || (e.KeyCode == Keys.Insert && e.Modifiers == Keys.Shift)) { e.Handled = true; Paste(); return; } if(e.KeyCode == Keys.Z && e.Modifiers == Keys.Control) { e.Handled = true; Undo(); return; } if(e.KeyCode == Keys.Y && e.Modifiers == Keys.Control) { e.Handled = true; Redo(); return; } if(e.KeyCode == Keys.Tab) { SelectedText = "\t"; return; } } bool IsNextLineBreak() { if(SelectionLength != 0) return false; string s = Text; if(SelectionStart + 2 > TextLength) return false; string s2 = s.Substring(SelectionStart, 2); if(s2 == "\r\n") return true; else return false; } bool IsBeforeLineBreak() { string s = Text; if(SelectionStart - 2 < 0) return false; string s2 = s.Substring(SelectionStart - 2, 2); if(s2 == "\r\n") return true; else return false; } void Back() { if(SelectionLength > 0) { SelectedText = ""; } else { if(SelectionStart != 0) { if(IsBeforeLineBreak()) { SelectionStart -= 2; SelectionLength = 2; SelectedText = ""; } else { SelectionStart--; SelectionLength = 1; SelectedText = ""; } } } } void Delete() { if(SelectionLength > 0) { SelectedText = ""; } else { if(SelectionStart != TextLength) { if(IsNextLineBreak()) { SelectionLength = 2; SelectedText = ""; } else { SelectionLength = 1; SelectedText = ""; } } } } void BreakLine() { SelectedText = "\r\n"; } new string SelectedText { get { return base.SelectedText; } set { UndoBuf buf = new UndoBuf(); buf.DeleteStart = SelectionStart; buf.DeleteLength = SelectionLength; buf.DeleteString = SelectedText; buf.InsertStart = SelectionStart; buf.InsertLength = value.Length; buf.InsertString = value; redoBufs.Clear(); undoBufs.Insert(0, buf); base.SelectedText = value; } } new string Text { get { return base.Text; } set { undoBufs.Clear(); redoBufs.Clear(); base.Text = value; } } new void Undo() { if(undoBufs.Count == 0) return; UndoBuf buf = undoBufs[0]; undoBufs.RemoveAt(0); SelectionStart = buf.InsertStart; SelectionLength = buf.InsertLength; base.SelectedText = ""; SelectionStart = buf.DeleteStart; SelectionLength = 0; base.SelectedText = buf.DeleteString; redoBufs.Insert(0, buf); } void Redo() { if(redoBufs.Count == 0) return; UndoBuf buf = redoBufs[0]; redoBufs.RemoveAt(0); SelectionStart = buf.DeleteStart; SelectionLength = buf.DeleteLength; base.SelectedText = ""; SelectionStart = buf.InsertStart; SelectionLength = 0; base.SelectedText = buf.InsertString; undoBufs.Insert(0, buf); } new void Cut() { Clipboard.SetText(SelectedText); SelectedText = ""; } new void Paste() { string str = Clipboard.GetText(); SelectedText = str; } } public class UndoBuf { internal int DeleteStart { get; set; } = 0; internal int DeleteLength { get; set; } = 0; internal string DeleteString { get; set; } = ""; internal int InsertStart { get; set; } = 0; internal int InsertLength { get; set; } = 0; internal string InsertString { get; set; } = ""; } } |