-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAutoCompleteTextBox.cs
More file actions
154 lines (128 loc) · 4.33 KB
/
AutoCompleteTextBox.cs
File metadata and controls
154 lines (128 loc) · 4.33 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
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace SharpOcarina
{
public class AutoCompleteTextBox : TextBox
{
private ListBox listBox;
private Form parentForm;
private List<string> items = new List<string>();
public AutoCompleteTextBox()
{
listBox = new ListBox();
listBox.Visible = false;
listBox.MouseDown += (object s, MouseEventArgs me) => ChooseItem(me.Location);
this.TextChanged += OnTextChanged;
this.KeyDown += OnKeyDown;
this.LostFocus += (s, e) => HideListBox();
}
public void SetAutoCompleteItems(IEnumerable<string> source)
{
items = source.ToList();
}
protected override void OnCreateControl()
{
base.OnCreateControl();
parentForm = this.FindForm();
if (parentForm != null)
parentForm.Controls.Add(listBox);
}
private void OnTextChanged(object sender, EventArgs e)
{
string text = this.Text;
if (string.IsNullOrWhiteSpace(text))
{
HideListBox();
return;
}
var startsWith = items
.Where(x => x.StartsWith(text, StringComparison.OrdinalIgnoreCase))
.OrderBy(x => x);
var contains = items
.Where(x => !x.StartsWith(text, StringComparison.OrdinalIgnoreCase) &&
x.IndexOf(text, StringComparison.OrdinalIgnoreCase) >= 0)
.OrderBy(x => x);
var matches = startsWith.Concat(contains).ToList();
if (matches.Count == 0)
{
HideListBox();
return;
}
ShowListBox(matches);
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (!listBox.Visible) return;
if (e.KeyCode == Keys.Down)
{
if (listBox.SelectedIndex < listBox.Items.Count - 1)
listBox.SelectedIndex++;
e.Handled = true;
}
else if (e.KeyCode == Keys.Up)
{
if (listBox.SelectedIndex > 0)
listBox.SelectedIndex--;
e.Handled = true;
}
else if (e.KeyCode == Keys.Enter)
{
ChooseItem();
e.Handled = true;
}
else if (e.KeyCode == Keys.Escape)
{
HideListBox();
e.Handled = true;
}
}
private void ShowListBox(List<string> matches)
{
listBox.BeginUpdate();
listBox.Items.Clear();
foreach (var item in matches) listBox.Items.Add(item);
if (listBox.Items.Count > 0)
listBox.SelectedIndex = 0;
listBox.EndUpdate();
var point = this.Parent.PointToScreen(this.Location);
point = parentForm.PointToClient(point);
listBox.Location = new Point(point.X, point.Y + this.Height);
listBox.Width = this.Width;
int itemCount = matches.Count;
int maxVisibleItems = 8;
int height = Math.Min(itemCount, maxVisibleItems) * listBox.ItemHeight + listBox.ItemHeight;
if (height < listBox.ItemHeight)
height = listBox.ItemHeight;
listBox.Height = height + 2;
listBox.Visible = true;
listBox.BringToFront();
}
private void HideListBox()
{
listBox.Visible = false;
}
private void ChooseItem()
{
if (listBox.SelectedItem != null)
{
this.Text = listBox.SelectedItem.ToString();
this.SelectionStart = this.Text.Length;
}
HideListBox();
}
private void ChooseItem(Point clickPoint)
{
int idx = listBox.IndexFromPoint(clickPoint);
if (idx >= 0)
{
listBox.SelectedIndex = idx;
this.Text = listBox.SelectedItem.ToString();
this.SelectionStart = this.Text.Length;
}
HideListBox();
}
}
}