File size: 5,977 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Globalization
Imports System.Reflection

Namespace PropertyGridEx

    Public Class UIListboxEditor
        Inherits UITypeEditor

        Private bIsDropDownResizable As Boolean = False
        Private WithEvents oList As New ListBox
        Private oSelectedValue As Object = Nothing
        Private oEditorService As IWindowsFormsEditorService

        Public Overloads Overrides Function GetEditStyle(ByVal context As _
        ITypeDescriptorContext) As UITypeEditorEditStyle
            If Not context Is Nothing AndAlso Not context.Instance Is Nothing Then
                Dim attribute As UIListboxIsDropDownResizable = context.PropertyDescriptor.Attributes(GetType(UIListboxIsDropDownResizable))
                If attribute IsNot Nothing Then
                    bIsDropDownResizable = True
                End If
                Return UITypeEditorEditStyle.DropDown
            End If
            Return UITypeEditorEditStyle.None
        End Function

        Public Overrides ReadOnly Property IsDropDownResizable() As Boolean
            Get
                Return bIsDropDownResizable
            End Get
        End Property

        <RefreshProperties(RefreshProperties.All)> _
        Public Overrides Function EditValue(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal provider As System.IServiceProvider, ByVal value As Object) As Object
            If context Is Nothing OrElse provider Is Nothing _
            OrElse context.Instance Is Nothing Then
                Return MyBase.EditValue(provider, value)
            End If

            oEditorService = provider.GetService(GetType(IWindowsFormsEditorService))
            If oEditorService IsNot Nothing Then

                ' Get the Back reference to the Custom Property
                Dim oDescriptor As CustomProperty.CustomPropertyDescriptor = context.PropertyDescriptor
                Dim cp As CustomProperty = oDescriptor.CustomProperty

                ' Declare attributes
                Dim datasource As UIListboxDatasource
                Dim valuemember As UIListboxValueMember
                Dim displaymember As UIListboxDisplayMember

                ' Get attributes
                With context.PropertyDescriptor
                    datasource = .Attributes(GetType(UIListboxDatasource))
                    valuemember = .Attributes(GetType(UIListboxValueMember))
                    displaymember = .Attributes(GetType(UIListboxDisplayMember))
                End With

                With oList
                    .BorderStyle = BorderStyle.None
                    .IntegralHeight = True

                    If datasource IsNot Nothing Then
                        .DataSource = datasource.Value
                    End If

                    If displaymember IsNot Nothing Then
                        .DisplayMember = displaymember.Value
                    End If

                    If valuemember IsNot Nothing Then
                        .ValueMember = valuemember.Value
                    End If

                    If value IsNot Nothing Then
                        If value.GetType.Name = "String" Then
                            oList.Text = value
                        Else
                            oList.SelectedItem = value
                        End If
                    End If

                End With

                AddHandler oList.SelectedIndexChanged, AddressOf Me.SelectedItem

                oEditorService.DropDownControl(oList)
                If oList.SelectedIndices.Count = 1 Then
                    cp.SelectedItem = oList.SelectedItem
                    cp.SelectedValue = oSelectedValue
                    value = oList.Text
                End If
                oEditorService.CloseDropDown()
            Else
                Return MyBase.EditValue(provider, value)
            End If

            Return value

        End Function

        Private Sub SelectedItem(ByVal sender As Object, ByVal e As EventArgs)
            If oEditorService IsNot Nothing Then
                If oList.SelectedValue IsNot Nothing Then oSelectedValue = oList.SelectedValue
                oEditorService.CloseDropDown()
            End If
        End Sub

        Public Class UIListboxDatasource
            Inherits Attribute
            Private oDataSource As Object
            Public Sub New(ByRef Datasource As Object)
                oDataSource = Datasource
            End Sub
            Public ReadOnly Property Value() As Object
                Get
                    Return oDataSource
                End Get
            End Property
        End Class

        Public Class UIListboxValueMember
            Inherits Attribute
            Private sValueMember As String
            Public Sub New(ByVal ValueMember As String)
                sValueMember = ValueMember
            End Sub
            Public Property Value() As String
                Get
                    Return sValueMember
                End Get
                Set(ByVal value As String)
                    sValueMember = value
                End Set
            End Property
        End Class

        Public Class UIListboxDisplayMember
            Inherits Attribute
            Private sDisplayMember As String
            Public Sub New(ByVal DisplayMember As String)
                sDisplayMember = DisplayMember
            End Sub
            Public Property Value() As String
                Get
                    Return sDisplayMember
                End Get
                Set(ByVal value As String)
                    sDisplayMember = value
                End Set
            End Property

        End Class

        Public Class UIListboxIsDropDownResizable
            Inherits Attribute
        End Class

    End Class

End Namespace