-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTextBoxWrapper.cs
138 lines (115 loc) · 4.11 KB
/
TextBoxWrapper.cs
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
using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
namespace AutocompleteMenuNS
{
/// <summary>
/// Wrapper over the control like TextBox.
/// </summary>
public class TextBoxWrapper : ITextBoxWrapper
{
private Control target;
private PropertyInfo selectionStart;
private PropertyInfo selectionLength;
private PropertyInfo selectedText;
private PropertyInfo readonlyProperty;
private MethodInfo getPositionFromCharIndex;
private event ScrollEventHandler RTBScroll;
private TextBoxWrapper(Control targetControl)
{
this.target = targetControl;
Init();
}
protected virtual void Init()
{
var t = target.GetType();
selectedText = t.GetProperty("SelectedText");
selectionLength = t.GetProperty("SelectionLength");
selectionStart = t.GetProperty("SelectionStart");
readonlyProperty = t.GetProperty("ReadOnly");
getPositionFromCharIndex = t.GetMethod("GetPositionFromCharIndex") ?? t.GetMethod("PositionToPoint");
if (target is RichTextBox)
(target as RichTextBox).VScroll += new EventHandler(TextBoxWrapper_VScroll);
}
void TextBoxWrapper_VScroll(object sender, EventArgs e)
{
if (RTBScroll != null)
RTBScroll(sender, new ScrollEventArgs(ScrollEventType.EndScroll, 0, 1));
}
public static TextBoxWrapper Create(Control targetControl)
{
var result = new TextBoxWrapper(targetControl);
if (result.selectedText == null || result.selectionLength == null || result.selectionStart == null ||
result.getPositionFromCharIndex == null)
return null;
return result;
}
public virtual string Text
{
get { return target.Text; }
set { target.Text = value; }
}
public virtual string SelectedText
{
get { return (string) selectedText.GetValue(target, null); }
set { selectedText.SetValue(target, value, null); }
}
public virtual int SelectionLength
{
get { return (int) selectionLength.GetValue(target, null); }
set { selectionLength.SetValue(target, value, null); }
}
public virtual int SelectionStart
{
get { return (int) selectionStart.GetValue(target, null); }
set { selectionStart.SetValue(target, value, null); }
}
public virtual Point GetPositionFromCharIndex(int pos)
{
return (Point) getPositionFromCharIndex.Invoke(target, new object[] {pos});
}
public virtual Form FindForm()
{
return target.FindForm();
}
public virtual event EventHandler LostFocus
{
add { target.LostFocus += value; }
remove { target.LostFocus -= value; }
}
public virtual event ScrollEventHandler Scroll
{
add {
if(target is RichTextBox)
RTBScroll += value;
else
if(target is ScrollableControl)(target as ScrollableControl).Scroll += value;
}
remove {
if (target is RichTextBox)
RTBScroll -= value;
else
if(target is ScrollableControl)(target as ScrollableControl).Scroll -= value;
}
}
public virtual event KeyEventHandler KeyDown
{
add { target.KeyDown += value; }
remove { target.KeyDown -= value; }
}
public virtual event MouseEventHandler MouseDown
{
add { target.MouseDown += value; }
remove { target.MouseDown -= value; }
}
public virtual Control TargetControl
{
get { return target; }
}
public bool Readonly
{
get { return (bool) readonlyProperty.GetValue(target, null); }
}
}
}