A
Alessandro Zifiglio
I am trying to create expandable properties in the propertygrid for a custom
control i am building but it wont work. I have derived my TypeConverter
from ExpandableObjectoConverter. I have overridden :
CanConvertFrom,ConvertFrom,ConvertTo,CanConvertTo. I know exactly what these
do. I have read the docs thoroughly and properly implemented it all. Still
the properties thats supposed to be expandable arent for some reason. I have
been struggling with this for a few hours now. To simplify this i have
created a small test application with my implementation. Can anybody give it
a look and let me know where i went wrong and what i should be doing
instead.
Thanks
Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization
Imports System.Web.UI
<DefaultProperty("Text"), _
ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")>
_
Public Class WebCustomControl1
Inherits System.Web.UI.WebControls.WebControl
Dim _text As String
<Bindable(True), Category("Appearance"), DefaultValue("")> Property
[Text]() As String
Get
Return _text
End Get
Set(ByVal Value As String)
_text = Value
End Set
End Property
Protected Overrides Sub Render(ByVal output As
System.Web.UI.HtmlTextWriter)
output.Write([Text])
End Sub
End Class
<TypeConverter(GetType(ExpandablePropertiesTypeConverter)),
Category("Appearance"), _
DescriptionAttribute("Two expandable properties")> _
Public Class ExpandableProperties
Private _sampleValue1 As Boolean = True
Private _sampleValue2 As Boolean = True
<DefaultValueAttribute(True), _
Description("Expandable property 2")> _
Public Property [sampleValue1]() As Boolean
Get
Return _sampleValue1
End Get
Set(ByVal Value As Boolean)
_sampleValue1 = Value
End Set
End Property
<DefaultValueAttribute(True), _
Description("Expandable property 2")> _
Public Property [sampleValue2]() As Boolean
Get
Return _sampleValue2
End Get
Set(ByVal Value As Boolean)
_sampleValue2 = Value
End Set
End Property
End Class
------------ Then in a seperate file i got the type converter class:
ExpandablePropertiesTypeConverter.vb ----------------------------
Imports System.ComponentModel
Imports System.Globalization
Public Class ExpandablePropertiesTypeConverter : Inherits
ExpandableObjectConverter
' Overrides the CanConvertFrom method of TypeConverter.
Public Overloads Overrides Function CanConvertFrom(ByVal context As
ITypeDescriptorContext, ByVal sourceType As Type) As Boolean
If sourceType Is GetType(String) Then
Return True
End If
Return MyBase.CanConvertFrom(context, sourceType)
End Function
' Overrides the ConvertFrom method of TypeConverter.
Public Overloads Overrides Function ConvertFrom(ByVal context As
ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object)
As Object
If (TypeOf value Is String) Then
Try
Dim s As String = CStr(value)
Dim colon As Integer = s.IndexOf(":")
Dim comma As Integer = s.IndexOf(",")
If (colon <> -1 AndAlso comma <> -1) Then
Dim samplevalue1 As String = s.Substring(colon + 1, _
(comma - colon - 1))
colon = s.IndexOf(":", comma + 1)
comma = s.IndexOf(",", comma + 1)
Dim samplevalue2 As String = s.Substring(colon + 1, _
(comma - colon - 1))
Dim control1 As ExpandableProperties = New
ExpandableProperties()
control1.sampleValue1 = Boolean.Parse(samplevalue1)
control1.sampleValue2 = Boolean.Parse(samplevalue2)
Return control1
End If
Catch
Throw New ArgumentException( _
"Can not convert '" & CStr(value) & _
"' to type windowAttributes")
End Try
End If
Return MyBase.ConvertFrom(context, culture, value)
End Function
' Overrides the ConvertTo method of TypeConverter.
Public Overloads Overrides Function ConvertTo(ByVal context As
ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object,
ByVal destinationType As Type) As Object
If (destinationType Is GetType(System.String) _
AndAlso TypeOf value Is ExpandableProperties) Then
Dim control1 As ExpandableProperties = CType(value,
ExpandableProperties)
Return "Sample1:" & control1.sampleValue1 & ", Sample2:" &
control1.sampleValue2
End If
Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function
Public Overloads Overrides Function CanConvertTo( _
ByVal context As ITypeDescriptorContext, _
ByVal destinationType As Type) As Boolean
If (destinationType Is GetType(ExpandableProperties)) Then
Return True
End If
Return MyBase.CanConvertFrom(context, destinationType)
End Function
End Class
control i am building but it wont work. I have derived my TypeConverter
from ExpandableObjectoConverter. I have overridden :
CanConvertFrom,ConvertFrom,ConvertTo,CanConvertTo. I know exactly what these
do. I have read the docs thoroughly and properly implemented it all. Still
the properties thats supposed to be expandable arent for some reason. I have
been struggling with this for a few hours now. To simplify this i have
created a small test application with my implementation. Can anybody give it
a look and let me know where i went wrong and what i should be doing
instead.
Thanks
Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization
Imports System.Web.UI
<DefaultProperty("Text"), _
ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")>
_
Public Class WebCustomControl1
Inherits System.Web.UI.WebControls.WebControl
Dim _text As String
<Bindable(True), Category("Appearance"), DefaultValue("")> Property
[Text]() As String
Get
Return _text
End Get
Set(ByVal Value As String)
_text = Value
End Set
End Property
Protected Overrides Sub Render(ByVal output As
System.Web.UI.HtmlTextWriter)
output.Write([Text])
End Sub
End Class
<TypeConverter(GetType(ExpandablePropertiesTypeConverter)),
Category("Appearance"), _
DescriptionAttribute("Two expandable properties")> _
Public Class ExpandableProperties
Private _sampleValue1 As Boolean = True
Private _sampleValue2 As Boolean = True
<DefaultValueAttribute(True), _
Description("Expandable property 2")> _
Public Property [sampleValue1]() As Boolean
Get
Return _sampleValue1
End Get
Set(ByVal Value As Boolean)
_sampleValue1 = Value
End Set
End Property
<DefaultValueAttribute(True), _
Description("Expandable property 2")> _
Public Property [sampleValue2]() As Boolean
Get
Return _sampleValue2
End Get
Set(ByVal Value As Boolean)
_sampleValue2 = Value
End Set
End Property
End Class
------------ Then in a seperate file i got the type converter class:
ExpandablePropertiesTypeConverter.vb ----------------------------
Imports System.ComponentModel
Imports System.Globalization
Public Class ExpandablePropertiesTypeConverter : Inherits
ExpandableObjectConverter
' Overrides the CanConvertFrom method of TypeConverter.
Public Overloads Overrides Function CanConvertFrom(ByVal context As
ITypeDescriptorContext, ByVal sourceType As Type) As Boolean
If sourceType Is GetType(String) Then
Return True
End If
Return MyBase.CanConvertFrom(context, sourceType)
End Function
' Overrides the ConvertFrom method of TypeConverter.
Public Overloads Overrides Function ConvertFrom(ByVal context As
ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object)
As Object
If (TypeOf value Is String) Then
Try
Dim s As String = CStr(value)
Dim colon As Integer = s.IndexOf(":")
Dim comma As Integer = s.IndexOf(",")
If (colon <> -1 AndAlso comma <> -1) Then
Dim samplevalue1 As String = s.Substring(colon + 1, _
(comma - colon - 1))
colon = s.IndexOf(":", comma + 1)
comma = s.IndexOf(",", comma + 1)
Dim samplevalue2 As String = s.Substring(colon + 1, _
(comma - colon - 1))
Dim control1 As ExpandableProperties = New
ExpandableProperties()
control1.sampleValue1 = Boolean.Parse(samplevalue1)
control1.sampleValue2 = Boolean.Parse(samplevalue2)
Return control1
End If
Catch
Throw New ArgumentException( _
"Can not convert '" & CStr(value) & _
"' to type windowAttributes")
End Try
End If
Return MyBase.ConvertFrom(context, culture, value)
End Function
' Overrides the ConvertTo method of TypeConverter.
Public Overloads Overrides Function ConvertTo(ByVal context As
ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object,
ByVal destinationType As Type) As Object
If (destinationType Is GetType(System.String) _
AndAlso TypeOf value Is ExpandableProperties) Then
Dim control1 As ExpandableProperties = CType(value,
ExpandableProperties)
Return "Sample1:" & control1.sampleValue1 & ", Sample2:" &
control1.sampleValue2
End If
Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function
Public Overloads Overrides Function CanConvertTo( _
ByVal context As ITypeDescriptorContext, _
ByVal destinationType As Type) As Boolean
If (destinationType Is GetType(ExpandableProperties)) Then
Return True
End If
Return MyBase.CanConvertFrom(context, destinationType)
End Function
End Class