File size: 11,741 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 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 |
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing.Design
Imports System.Windows.Forms
Imports System.Xml.Serialization
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization.Formatters
Imports System.IO
Namespace PropertyGridEx
<Serializable()> _
Public Class CustomPropertyCollection
Inherits System.Collections.CollectionBase
Implements ICustomTypeDescriptor
#Region "Collection related methods"
Public Overridable Function Add(ByVal value As CustomProperty) As Integer
Return MyBase.List.Add(value)
End Function
Public Overridable Function Add(ByVal strName As String, ByVal objValue As Object, Optional ByVal boolIsReadOnly As Boolean = True, Optional ByVal strCategory As String = "", Optional ByVal strDescription As String = "", Optional ByVal boolVisible As Boolean = True) As Integer
Return MyBase.List.Add(New CustomProperty(strName, objValue, boolIsReadOnly, strCategory, strDescription, boolVisible))
End Function
Public Overridable Function Add(ByVal strName As String, ByRef objRef As Object, ByVal strProp As String, Optional ByVal boolIsReadOnly As Boolean = True, Optional ByVal strCategory As String = "", Optional ByVal strDescription As String = "", Optional ByVal boolVisible As Boolean = True) As Integer
Return MyBase.List.Add(New CustomProperty(strName, objRef, strProp, boolIsReadOnly, strCategory, strDescription, boolVisible))
End Function
Default Public Overloads ReadOnly Property Item(ByVal index As Integer) As CustomProperty
Get
Return DirectCast(MyBase.List.Item(index), CustomProperty)
End Get
End Property
Public Function FindItem(ByVal label As String) As Integer
Dim i As Integer = 0
Dim CustomProp As CustomProperty
For Each CustomProp In MyBase.List
If CustomProp.Name = label Then
Return i
End If
i = i + 1
Next
End Function
Public Overridable Sub Remove(ByVal Name As String)
Dim CustomProp As CustomProperty
For Each CustomProp In MyBase.List
If CustomProp.Name = Name Then
MyBase.List.Remove(CustomProp)
Return
End If
Next
End Sub
#End Region
#Region "Implements ICustomTypeDescriptor"
Public Function GetAttributes() As System.ComponentModel.AttributeCollection Implements System.ComponentModel.ICustomTypeDescriptor.GetAttributes
Return TypeDescriptor.GetAttributes(Me, True)
End Function
Public Function GetClassName() As String Implements System.ComponentModel.ICustomTypeDescriptor.GetClassName
Return TypeDescriptor.GetClassName(Me, True)
End Function
Public Function GetComponentName() As String Implements System.ComponentModel.ICustomTypeDescriptor.GetComponentName
Return TypeDescriptor.GetComponentName(Me, True)
End Function
Public Function GetConverter() As System.ComponentModel.TypeConverter Implements System.ComponentModel.ICustomTypeDescriptor.GetConverter
Return TypeDescriptor.GetConverter(Me, True)
End Function
Public Function GetDefaultEvent() As System.ComponentModel.EventDescriptor Implements System.ComponentModel.ICustomTypeDescriptor.GetDefaultEvent
Return TypeDescriptor.GetDefaultEvent(Me, True)
End Function
Public Function GetDefaultProperty() As System.ComponentModel.PropertyDescriptor Implements System.ComponentModel.ICustomTypeDescriptor.GetDefaultProperty
Return TypeDescriptor.GetDefaultProperty(Me, True)
End Function
Public Function GetEditor(ByVal editorBaseType As System.Type) As Object Implements System.ComponentModel.ICustomTypeDescriptor.GetEditor
Return TypeDescriptor.GetEditor(Me, editorBaseType, True)
End Function
Public Function GetEvents() As System.ComponentModel.EventDescriptorCollection Implements System.ComponentModel.ICustomTypeDescriptor.GetEvents
Return TypeDescriptor.GetEvents(Me, True)
End Function
Public Function GetEvents(ByVal attributes() As System.Attribute) As System.ComponentModel.EventDescriptorCollection Implements System.ComponentModel.ICustomTypeDescriptor.GetEvents
Return TypeDescriptor.GetEvents(Me, attributes, True)
End Function
Public Function GetProperties() As System.ComponentModel.PropertyDescriptorCollection Implements System.ComponentModel.ICustomTypeDescriptor.GetProperties
Return TypeDescriptor.GetProperties(Me, True)
End Function
Public Function GetProperties(ByVal attributes() As System.Attribute) As System.ComponentModel.PropertyDescriptorCollection Implements System.ComponentModel.ICustomTypeDescriptor.GetProperties
Dim Properties As New PropertyDescriptorCollection(Nothing)
Try
Dim CustomProp As CustomProperty
For Each CustomProp In MyBase.List
If CustomProp.Visible Then
Dim attrs As ArrayList = New ArrayList()
' Expandable Object Converter
If CustomProp.IsBrowsable Then
attrs.Add(New TypeConverterAttribute(GetType(BrowsableTypeConverter)))
End If
' The Filename Editor
If CustomProp.UseFileNameEditor = True Then
attrs.Add(New EditorAttribute(GetType(UIFilenameEditor), GetType(UITypeEditor)))
End If
' Custom Choices Type Converter
If CustomProp.Choices IsNot Nothing Then
attrs.Add(New TypeConverterAttribute(GetType(CustomChoices.CustomChoicesTypeConverter)))
End If
' Password Property
If CustomProp.IsPassword Then
attrs.Add(New PasswordPropertyTextAttribute(True))
End If
' Parenthesize Property
If CustomProp.Parenthesize Then
attrs.Add(New ParenthesizePropertyNameAttribute(True))
End If
' Datasource
If CustomProp.Datasource IsNot Nothing Then
attrs.Add(New EditorAttribute(GetType(UIListboxEditor), GetType(UITypeEditor)))
End If
' Custom Editor
If CustomProp.CustomEditor IsNot Nothing Then
attrs.Add(New EditorAttribute(CustomProp.CustomEditor.GetType, GetType(UITypeEditor)))
End If
' Custom Type Converter
If CustomProp.CustomTypeConverter IsNot Nothing Then
attrs.Add(New TypeConverterAttribute(CustomProp.CustomTypeConverter.GetType))
End If
' Is Percentage
If CustomProp.IsPercentage Then
attrs.Add(New TypeConverterAttribute(GetType(OpacityConverter)))
End If
' 3-dots button event delegate
If CustomProp.OnClick IsNot Nothing Then
attrs.Add(New EditorAttribute(GetType(UICustomEventEditor), GetType(UITypeEditor)))
End If
' Default value attribute
If CustomProp.DefaultValue IsNot Nothing Then
attrs.Add(New DefaultValueAttribute(CustomProp.Type, CustomProp.Value.ToString))
Else
' Default type attribute
If CustomProp.DefaultType IsNot Nothing Then
attrs.Add(New DefaultValueAttribute(CustomProp.DefaultType, Nothing))
End If
End If
' Extra Attributes
If CustomProp.Attributes IsNot Nothing Then
attrs.AddRange(CustomProp.Attributes)
End If
' Add my own attributes
Dim attrArray As Attribute() = attrs.ToArray(GetType(Attribute))
Properties.Add(New CustomProperty.CustomPropertyDescriptor(CustomProp, attrArray))
End If
Next
Catch ex As Exception
End Try
Return Properties
End Function
Public Function GetPropertyOwner(ByVal pd As System.ComponentModel.PropertyDescriptor) As Object Implements System.ComponentModel.ICustomTypeDescriptor.GetPropertyOwner
Return Me
End Function
#End Region
#Region "Serialize & Deserialize related methods"
Public Sub SaveXml(ByVal filename As String)
Dim serializer As New XmlSerializer(GetType(CustomPropertyCollection))
Dim writer As New FileStream(filename, FileMode.Create)
Try
serializer.Serialize(writer, Me)
Catch ex As Exception
MsgBox(ex.InnerException.Message)
End Try
writer.Close()
End Sub
Public Function LoadXml(ByVal filename As String) As Boolean
Try
Dim serializer As New XmlSerializer(GetType(CustomPropertyCollection))
Dim reader As New FileStream(filename, FileMode.Open)
Dim cpc As CustomPropertyCollection = CType(serializer.Deserialize(reader), CustomPropertyCollection)
For Each customprop As CustomProperty In cpc
customprop.RebuildAttributes()
Me.Add(customprop)
Next
cpc = Nothing
reader.Close()
Return True
Catch ex As Exception
Return False
End Try
End Function
Public Sub SaveBinary(ByVal filename As String)
Dim stream As Stream = File.Create(filename)
Dim serializer As New BinaryFormatter
Try
serializer.Serialize(stream, Me)
Catch ex As Exception
MsgBox(ex.InnerException.Message)
End Try
stream.Close()
End Sub
Public Function LoadBinary(ByVal filename As String) As Boolean
Try
Dim stream As Stream = File.Open(filename, FileMode.Open)
Dim formatter As New BinaryFormatter()
If stream.Length > 0 Then
Dim cpc As CustomPropertyCollection = CType(formatter.Deserialize(stream), CustomPropertyCollection)
For Each customprop As CustomProperty In cpc
customprop.RebuildAttributes()
Me.Add(customprop)
Next
cpc = Nothing
stream.Close()
Return True
Else
stream.Close()
Return False
End If
Catch ex As Exception
Return False
End Try
End Function
#End Region
End Class
End Namespace
|