-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomControlBinder.cs
86 lines (78 loc) · 3.92 KB
/
CustomControlBinder.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
namespace CustomFontEditControl
{
static class CustomControlBinder
{
// lsit of initial controls
static List<ListBoxControl> ListOfInitialControls = new List<ListBoxControl>();
public static void AssignControls(ListBoxControl InitialControl, TextEdit DependControl, bool ChangeDependControlValueOnLeaveFocus)
{
if (InitialControl != null && DependControl != null)
{
InitialControl.Tag = DependControl; DependControl.Tag = InitialControl;
ListOfInitialControls.Add(InitialControl);
DependControl.Properties.EditValueChanging += new DevExpress.XtraEditors.Controls.ChangingEventHandler(TextEdit_EditValueChanging);
if (ChangeDependControlValueOnLeaveFocus) DependControl.Properties.Leave += new EventHandler(TextEdit_Leave);
DependControl.Properties.KeyDown += new System.Windows.Forms.KeyEventHandler(TextEdit_KeyDown);
InitialControl.SelectedIndexChanged += new EventHandler(ListBoxControl_SelectedIndexChanged);
}
}
static void ListBoxControl_SelectedIndexChanged(object sender, EventArgs e)
{
ListBoxControl currentListBox = sender as ListBoxControl;
TextEdit currentTextEdit = currentListBox.Tag as TextEdit;
if (currentListBox.Focused)
{
currentTextEdit.EditValue = currentListBox.SelectedValue;
if (TextValueChanged != null) TextValueChanged(currentTextEdit, new EventArgs());
}
}
static void TextEdit_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
TextEdit currentTextEdit = sender as TextEdit;
ListBoxControl currentListBox = currentTextEdit.Tag as ListBoxControl;
if (e.KeyValue == 40)
{
currentListBox.Focus();
}
}
static void TextEdit_Leave(object sender, EventArgs e)
{
TextEdit currentTextEdit = sender as TextEdit;
ListBoxControl currentListBox = currentTextEdit.Tag as ListBoxControl;
currentTextEdit.EditValue = currentListBox.SelectedValue;
if (TextValueChanged != null) TextValueChanged(currentTextEdit, new EventArgs());
}
static void TextEdit_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
{
TextEdit currentTextEdit = sender as TextEdit;
ListBoxControl currentListBox = currentTextEdit.Tag as ListBoxControl;
if (e.NewValue != null)
{
int iSelectedIndex = currentListBox.FindString(e.NewValue.ToString());
if (iSelectedIndex >= 0 && iSelectedIndex < currentListBox.Items.Count)
{
currentListBox.SelectedIndex = iSelectedIndex;
}
}
}
internal delegate void CustomControlBinderrEventHandler(object sender, EventArgs e);
public static event CustomControlBinderrEventHandler TextValueChanged;
public static void DisposeControlBinder()
{
foreach (ListBoxControl currentListBox in ListOfInitialControls)
{
TextEdit currentTextEdit = currentListBox.Tag as TextEdit;
currentTextEdit.Properties.EditValueChanging -= new DevExpress.XtraEditors.Controls.ChangingEventHandler(TextEdit_EditValueChanging);
currentTextEdit.Properties.Leave -= new EventHandler(TextEdit_Leave);
currentTextEdit.Properties.KeyDown -= new System.Windows.Forms.KeyEventHandler(TextEdit_KeyDown);
currentListBox.SelectedIndexChanged -= new EventHandler(ListBoxControl_SelectedIndexChanged);
}
ListOfInitialControls.Clear();
}
}
}