/
EditableListBox.cs
374 lines (310 loc) · 10.6 KB
/
EditableListBox.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright © 2010 Phil Booth
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace PB
{
/// <summary>
/// An intuitive editable ListBox control. Edit mode is accessed by
/// double-clicking on the control or pressing Enter when an item
/// is selected. Once you're in edit mode, the up and down cursor
/// keys can be used to cycle through the different items without
/// committing any changes that are made. Tab and Shift+Tab can be
/// used to cycle through the items, committing any changes that
/// are made as you go along. Pressing Enter will commit changes
/// and leave edit mode. Pressing Esc or simply navigating away from
/// the edit region will leave edit mode without committing changes.
/// </summary>
[Category("PB Controls"), Description("An intuitive, editable list box control.")]
public partial class EditableListBox : UserControl
{
public EditableListBox()
{
InitializeComponent();
listboxHelper = new ListboxHelper(listbox);
textboxHelper = new TextboxHelper(textbox);
buttonsHelper = new ButtonsHelper(new Button[5] { buttonSort, buttonUp, buttonDown, buttonAdd, buttonRemove });
}
#region Properties
[Description("Indicates whether the EditableListBox will display a horizontal scroll bar for items beyond the right edge of the EditableListBox.")]
public bool HorizontalScrollbar
{
get { return listboxHelper.horizontalScroll; }
set { listboxHelper.horizontalScroll = value; }
}
[Description("Indicates the width, in pixels, by which an EditableListBox can be scrolled horizontally. Only valid if HorizontalScrollbar is true.")]
public int HorizontalExtent
{
get { return listboxHelper.horizontalExtent; }
set { listboxHelper.horizontalExtent = value; }
}
[Description("Indicates whether the EditableListBox should always have a scroll bar present, regardless of how many items are in it.")]
public bool ScrollAlwaysVisible
{
get { return listboxHelper.alwaysShowScroll; }
set { listboxHelper.alwaysShowScroll = value; }
}
[Description("Indicates whether values should be displayed in columns horizontally.")]
public bool MultiColumn
{
get { return listboxHelper.multiColumn; }
set { listboxHelper.multiColumn = value; }
}
[Description("Indicates how wide each column should be in a multi-column EditableListBox.")]
public int ColumnWidth
{
get { return listboxHelper.columnWidth; }
set { listboxHelper.columnWidth = value; }
}
[Description("Indicates whether tab characters should be expanded to spaces.")]
public bool UseTabStops
{
get { return listboxHelper.showTabs; }
set { listboxHelper.showTabs = value; }
}
[Description("Indicates whether items in the EditableListBox should be sorted alpha-numerically.")]
public bool Sorted
{
get { return listboxHelper.isSorted; }
set { listboxHelper.isSorted = value; }
}
[Description("The collection of items in the EditableListBox.")]
public ListBox.ObjectCollection Items
{
get { return listboxHelper.items; }
}
[Description("Gets or sets the zero-based index of the currently selected item in the EditableListBox.")]
public int SelectedIndex
{
get { return listboxHelper.selectedIndex; }
set { listboxHelper.selectedIndex = value; }
}
[Description("Indicates the size of the EditableListBox buttons.")]
public SimpleSize ButtonSize
{
get { return buttonsHelper.ButtonSize; }
set
{
if(buttonsHelper.ButtonSize != value)
{
buttonsHelper.ButtonSize = value;
if(value == SimpleSize.Large)
{
buttonsHelper.icons = imagelistLargeButtons;
listboxHelper.reduceHeight(ButtonsHelper.dimensionsDiff);
}
else
{
buttonsHelper.icons = imagelistSmallButtons;
listboxHelper.increaseHeight(ButtonsHelper.dimensionsDiff);
}
}
}
}
[Description("Indicates whether the EditableListBox is empty or populated.")]
public bool Empty
{
get { return listboxHelper.isEmpty; }
}
[Description("Indicates the value of the currently selected item in the EditableListBox.")]
public string SelectedItem
{
get { return listboxHelper.selectedItem; }
//set { listboxHelper.selectedItem = value; }
}
#endregion
#region Events
#region Data events
public class DataEventArgs : EventArgs
{
public DataEventArgs(int itemIndex, string item)
{
ItemIndex = itemIndex;
Item = item;
}
public readonly int ItemIndex;
public readonly string Item;
}
public delegate void DataEventHandler(object sender, DataEventArgs evt);
[Category("Data"), Description("Occurs when a new item is added to the EditableListBox.")]
public event DataEventHandler AddItem;
[Category("Data"), Description("Occurs when an item is removed from the EditableListBox.")]
public event DataEventHandler RemoveItem;
[Category("Data"), Description("Occurs when an item in the EditableListBox is moved up.")]
public event DataEventHandler ShiftUpItem;
[Category("Data"), Description("Occurs when an item in the EditableListBox is moved down.")]
public event DataEventHandler ShiftDownItem;
[Category("Data"), Description("Occurs when an item in the EditableListBox is edited.")]
public event DataEventHandler EditItem;
private void fireDataEvent(DataEventHandler handler, int itemIndex, string item)
{
if(handler != null)
handler(this, new DataEventArgs(itemIndex, item));
}
#endregion
#region Sort events
public class SortEventArgs : EventArgs
{
public SortEventArgs(SortOrder order)
{
Order = order;
}
public readonly SortOrder Order;
}
public delegate void SortEventHandler(object sender, SortEventArgs evt);
[Category("Sort"), Description("Occurs when items in the EditableListBox are sorted or unsorted.")]
public event SortEventHandler SortItems;
private void fireSortEvent(SortOrder order)
{
if(SortItems != null)
SortItems(this, new SortEventArgs(order));
}
#endregion
#endregion
#region Key handling
protected override bool ProcessDialogKey(Keys keyData)
{
if(textbox.Focused)
{
if(onTextboxKey(keyData))
return true;
}
else if(listbox.Focused)
{
if(onListboxKey(keyData))
return true;
}
return base.ProcessDialogKey(keyData);
}
private bool onTextboxKey(Keys keyData)
{
switch(keyData)
{
case Keys.Enter:
updateItem();
goto case Keys.Escape;
case Keys.Escape:
finishEditing();
listboxHelper.focus();
return true;
case Keys.Tab:
updateItem();
goto case Keys.Down;
case Keys.Down:
nextItem();
editItem();
return true;
case Keys.Shift | Keys.Tab:
updateItem();
goto case Keys.Up;
case Keys.Up:
previousItem();
editItem();
return true;
}
return false;
}
private bool onListboxKey(Keys keyData)
{
switch(keyData)
{
case Keys.Enter:
case Keys.Space:
editItem();
return true;
}
return false;
}
#endregion
#region Mouse handling
private void listbox_DoubleClick(object sender, EventArgs e)
{
selectItem(((MouseEventArgs)e).Location);
fireDataEvent(AddItem, listboxHelper.selectedIndex, listboxHelper.selectedItem);
editItem();
}
private void buttonSort_Click(object sender, EventArgs e)
{
listboxHelper.sort();
fireSortEvent(listboxHelper.order);
}
private void buttonUp_Click(object sender, EventArgs e)
{
fireDataEvent(ShiftUpItem, listboxHelper.selectedIndex, listboxHelper.selectedItem);
listboxHelper.raiseSelectedItem();
}
private void buttonDown_Click(object sender, EventArgs e)
{
fireDataEvent(ShiftDownItem, listboxHelper.selectedIndex, listboxHelper.selectedItem);
listboxHelper.lowerSelectedItem();
}
private void buttonAdd_Click(object sender, EventArgs e)
{
listboxHelper.addNewItem();
fireDataEvent(AddItem, listboxHelper.selectedIndex, listboxHelper.selectedItem);
editItem();
}
private void buttonRemove_Click(object sender, EventArgs e)
{
fireDataEvent(RemoveItem, listboxHelper.selectedIndex, listboxHelper.selectedItem);
listboxHelper.removeItem();
}
private void textbox_Leave(object sender, EventArgs e)
{
updateItem();
finishEditing();
}
#endregion
#region Editing
private void selectItem(Point point)
{
listboxHelper.selectItem(point);
}
private void editItem()
{
textboxHelper.show(new Rectangle(listboxHelper.selectionRectangle.X + listboxHelper.location.X, listboxHelper.selectionRectangle.Y + listboxHelper.location.Y, listboxHelper.selectionRectangle.Width, listboxHelper.selectionRectangle.Height), listboxHelper.selectedItem);
}
private void finishEditing()
{
textboxHelper.hide();
}
private void updateItem()
{
fireDataEvent(EditItem, listboxHelper.selectedIndex, listboxHelper.selectedItem);
listboxHelper.updateSelectedItem(textbox.Text);
}
private void nextItem()
{
listboxHelper.selectNextItem();
}
private void previousItem()
{
listboxHelper.selectPreviousItem();
}
#endregion
public enum SimpleSize { Large, Small };
public enum SortOrder { SORTED, REVERSESORTED, UNSORTED };
private ListboxHelper listboxHelper;
private TextboxHelper textboxHelper;
private ButtonsHelper buttonsHelper;
}
}