Expandable properties in propertygrid not working

  • Thread starter Alessandro Zifiglio
  • Start date
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
 
A

Alessandro Zifiglio

Just in case nobody is looking at this thinking I want someone to make
corrections to my code, this is not the case. The reason I put sample code
is to show that I have followed whatever documentation microsoft had on
expandable properties and its not working for me. Has anybody dealt with
this before. That is when trying to implement expandable properties and it
dont work. I dont end up with errors either, those properties never show in
the propertygrid. Not as expandable properties or the normal expanded
properties. Just nada.

Alessandro Zifiglio said:
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> said:
_
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
 
A

Alessandro Zifiglio

Will somebody from Microsoft reply. It is important that I be able to
implement expandable properties. If I've been doing it right then maybe
someone knows of a workaround I could use to get this running for me? For
all I can think of right now is that this could be a bug with the vb.net
version.
I'm already behind schedule. I am desperate.
Microsoft !!!
 
J

Jimmy [Used-Disks]

Public Class WebCustomControl1

Where do you declare the property?

Also, though a TypeConverter may be useful, it looks like
ExpandableObjectConverter will do just fine in your case. There is no need
to extend it from what I can see.
 
A

Alessandro Zifiglio

Why am i being ignored. I see the guys from microsoft responding but wont
aknowledge my post.

It cant be so hard to reproduce my problem. I even included a sample project
to simplify. Still no answer
 
A

Alessandro Zifiglio

Its all there in my sample code. I have to extend it. I need to extend. I
want to overridable properties of the the expandableobjectconverter.
Otherwise my properties wont be editable and i cant use a custom display as
items are selected in the sub properties.
However I had already tried doing it simply as you suggested before posting
for the sake of experimenting and even that didnt wont work for me.
Have you been able to implement expandable properties in vb.net and has it
worked for you?
For all i can think of right now, this is a bug. I'm using the older version
of the framework because my version of VS.NET is the 2002 version and it
wont work with ASP.net 1.1

I'm not sure if my problem is a bug and has already been fixed with the
latest release of the the framework 1.1 version, if this is so i'd like the
confirmation and what i need to do to get this working with my current
version of the framework and not have to upgrade because that would mean my
purchasing vs.net 2003.
 
B

Ben Schwehn

I am trying to create expandable properties in the propertygrid for a custom
control i am building but it wont work.

Allessandro,

perhaps this is stupid, and you seem stressed so don't get angry if it is
indeed stupid, but don't you have to use the
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
attribute thing as well?
never done a converter but just a simple expandable property and that needed
this attribute to work...

hth
ben
 
A

Alessandro Zifiglio

hi ben,
The DesignerSerializationVisibility attribute class is useful in cases you
want to serialize the contents of your read/write property value.
However I'll do anything right now to get this working and tried adding that
attribute anyway. It didnt change a thing for me.
Thanks for trying to help, i appreciate it.

If only someone from microsoft would confirm this to be a bug in their
vb.net version and put me on the right track.
 
B

Ben Schwehn

Hello Allessandro,

I had a look at your sample code and if I use

Dim _test As ExpandableProperties
<Bindable(True), Category("Appearance"), DefaultValue("")> Property Test() As ExpandableProperties
Get
Return _test
End Get
Set(ByVal Value As ExpandableProperties)
_test = Value
End Set
End Property

in your websustomcontrol1

and change your convertfrom to

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 control1 As ExpandableProperties = New ExpandableProperties()
control1.sampleValue1 = True
control1.sampleValue2 = False

Return control1


Catch
Throw New ArgumentException( _
"Can not convert '" & CStr(value) & _
"' to type windowAttributes")

End Try
End If
Return MyBase.ConvertFrom(context, culture, value)

End Function

then the test property is displayed in the property grid as not expandable
firtst (because its not inititlised?) but
if you enter a string it uses the ConvertFrom function to set sampleValue1
to true etc and the Property is afterwards displayed as expandable

There seem to be some problems with you convertfrom function eg confusing
colon with komma at one point, perhaps there are more...

I'd first fix that and then perhaps initialise the test property

hth
ben
 
A

Alessandro Zifiglio

Be............................n!!!
You are the man. No.1-----------------------------Thank YOU!
For real, thanks for not giving up. The whole time I was thinking this was a
bug and never suspected that this could be my fault, and it was my fault. I
don't know how but I had forgotten to add that custom public property Test
you had just added into my webcustomcontrol class. That is why the
properties never showed up in the propertygrid in the first place. I don't
even know how I could have over looked this, because scenarios like this
have happened to me repeatedly, that is to have a piece of code missing like
that. I think its the stress. I've been sleeping very little and working too
much.
Oh and there's nothing wrong with the overridable functions of the
expandableobjectconverter. Its all correct and its working like a baby.
Thank you again for pointing out this mistake.


Ben Schwehn said:
Hello Allessandro,

I had a look at your sample code and if I use

Dim _test As ExpandableProperties
<Bindable(True), Category("Appearance"), DefaultValue("")> Property Test() As ExpandableProperties
Get
Return _test
End Get
Set(ByVal Value As ExpandableProperties)
_test = Value
End Set
End Property

in your websustomcontrol1

and change your convertfrom to

Public Overloads Overrides Function ConvertFrom(ByVal context As
ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object)
As Object
 
B

Ben Schwehn

[...]
I
don't know how but I had forgotten to add that custom public property Test
you had just added into my webcustomcontrol class.

The most obvious errors are sometimes the hardest to find...

[...]
that. I think its the stress. I've been sleeping very little and working too
much.

....especially when being stressed.

Well, there's one less thing to worry about :)
Thank you again for pointing out this mistake.

Glad I could help

bye,
Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,771
Messages
2,569,587
Members
45,099
Latest member
AmbrosePri
Top