forked from codeurzebs/CodeWalker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextBoxFix.cs
More file actions
94 lines (85 loc) · 2.64 KB
/
TextBoxFix.cs
File metadata and controls
94 lines (85 loc) · 2.64 KB
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
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace CodeWalker.WinForms
{
public partial class TextBoxFix : TextBox
{
bool ignoreChange = true;
List<string> storageUndo;
List<string> storageRedo;
public TextBoxFix()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
protected override void OnCreateControl()
{
base.OnCreateControl();
storageRedo = new List<string>();
storageUndo = new List<string> { Text };
ignoreChange = false;
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
//Fix for Ctrl-A/Z/Y not working in multiline textboxes.
const int WM_KEYDOWN = 0x100;
var keyCode = (Keys)(msg.WParam.ToInt32() & Convert.ToInt32(Keys.KeyCode));
if (msg.Msg == WM_KEYDOWN && (ModifierKeys == Keys.Control) && Focused)
{
if (keyCode == Keys.A)
{
SelectAll();
return true;
}
if (keyCode == Keys.Z)
{
Undo();
return true;
}
if (keyCode == Keys.Y)
{
Redo();
return true;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
if (!ignoreChange)
{
ClearUndo();
if (storageUndo.Count > 2048) storageUndo.RemoveAt(0);
if (storageRedo.Count > 2048) storageRedo.RemoveAt(0);
storageUndo.Add(Text);
}
}
public void Redo()
{
if (storageRedo.Count > 0)
{
ignoreChange = true;
Text = storageRedo[storageRedo.Count - 1];
storageUndo.Add(Text);
storageRedo.RemoveAt(storageRedo.Count - 1);
ignoreChange = false;
}
}
public new void Undo()
{
if (storageUndo.Count > 1)
{
ignoreChange = true;
Text = storageUndo[storageUndo.Count - 2];
storageRedo.Add(storageUndo[storageUndo.Count - 1]);
storageUndo.RemoveAt(storageUndo.Count - 1);
ignoreChange = false;
}
}
}
}