-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomControlBinder.vb
78 lines (70 loc) · 3.59 KB
/
CustomControlBinder.vb
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
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports DevExpress.XtraEditors
Namespace CustomFontEditControl
Friend NotInheritable Class CustomControlBinder
' lsit of initial controls
Private Shared ListOfInitialControls As New List(Of ListBoxControl)()
Private Sub New()
End Sub
Public Shared Sub AssignControls(ByVal InitialControl As ListBoxControl, ByVal DependControl As TextEdit, ByVal ChangeDependControlValueOnLeaveFocus As Boolean)
If InitialControl IsNot Nothing AndAlso DependControl IsNot Nothing Then
InitialControl.Tag = DependControl
DependControl.Tag = InitialControl
ListOfInitialControls.Add(InitialControl)
AddHandler DependControl.Properties.EditValueChanging, AddressOf TextEdit_EditValueChanging
If ChangeDependControlValueOnLeaveFocus Then
AddHandler DependControl.Properties.Leave, AddressOf TextEdit_Leave
End If
AddHandler DependControl.Properties.KeyDown, AddressOf TextEdit_KeyDown
AddHandler InitialControl.SelectedIndexChanged, AddressOf ListBoxControl_SelectedIndexChanged
End If
End Sub
Private Shared Sub ListBoxControl_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim currentListBox As ListBoxControl = TryCast(sender, ListBoxControl)
Dim currentTextEdit As TextEdit = TryCast(currentListBox.Tag, TextEdit)
If currentListBox.Focused Then
currentTextEdit.EditValue = currentListBox.SelectedValue
RaiseEvent TextValueChanged(currentTextEdit, New EventArgs())
End If
End Sub
Private Shared Sub TextEdit_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
Dim currentTextEdit As TextEdit = TryCast(sender, TextEdit)
Dim currentListBox As ListBoxControl = TryCast(currentTextEdit.Tag, ListBoxControl)
If e.KeyValue = 40 Then
currentListBox.Focus()
End If
End Sub
Private Shared Sub TextEdit_Leave(ByVal sender As Object, ByVal e As EventArgs)
Dim currentTextEdit As TextEdit = TryCast(sender, TextEdit)
Dim currentListBox As ListBoxControl = TryCast(currentTextEdit.Tag, ListBoxControl)
currentTextEdit.EditValue = currentListBox.SelectedValue
RaiseEvent TextValueChanged(currentTextEdit, New EventArgs())
End Sub
Private Shared Sub TextEdit_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs)
Dim currentTextEdit As TextEdit = TryCast(sender, TextEdit)
Dim currentListBox As ListBoxControl = TryCast(currentTextEdit.Tag, ListBoxControl)
If e.NewValue IsNot Nothing Then
Dim iSelectedIndex As Integer = currentListBox.FindString(e.NewValue.ToString())
If iSelectedIndex >= 0 AndAlso iSelectedIndex < currentListBox.Items.Count Then
currentListBox.SelectedIndex = iSelectedIndex
End If
End If
End Sub
Friend Delegate Sub CustomControlBinderrEventHandler(ByVal sender As Object, ByVal e As EventArgs)
Public Shared Event TextValueChanged As CustomControlBinderrEventHandler
Public Shared Sub DisposeControlBinder()
For Each currentListBox As ListBoxControl In ListOfInitialControls
Dim currentTextEdit As TextEdit = TryCast(currentListBox.Tag, TextEdit)
RemoveHandler currentTextEdit.Properties.EditValueChanging, AddressOf TextEdit_EditValueChanging
RemoveHandler currentTextEdit.Properties.Leave, AddressOf TextEdit_Leave
RemoveHandler currentTextEdit.Properties.KeyDown, AddressOf TextEdit_KeyDown
RemoveHandler currentListBox.SelectedIndexChanged, AddressOf ListBoxControl_SelectedIndexChanged
Next currentListBox
ListOfInitialControls.Clear()
End Sub
End Class
End Namespace