Override UITypeEditor on class

W

WALDO

Somebody asked this in August but didn't get a response.

I have a control with various properties, one of which returns type
Color.
I have a custom UITypeEditor which brings up the advanced ColorPicker
dialog. This UIType editor works fine if I change the property type to
String. When I switch it to Color, it displays a dropdown of the
KnownColor enumeration.

I did dome investigation and found that Color itself has a
UITypeEditor for the entire structure which negates my custom
UITypeEditor for my property. Since String doesn't have a UITypeEditor
(obviously, it is not necessary), Visual Studio looks at mine.

How can I convince Visual Studio to look at the UITypeEditor on the
property and not on the structure?

In the short term, I am converting the property back and forth from a
String, but this is not acceptible. Being that I cannot inherit from
Color (and I am using VB, so I don't have operators), creating a
subclass won't work.

Any ideas?
 
W

WALDO

OK. I figured it out.

I created a class that inherits from PropertyDescriptor. It takes an
existing property descriptor as an argument to its contructor. The
constructor shells to the base classes constructor specifying the
attributes that I want to use (In this case, the TypeEditorAttribute
specifying my UITypeEditor). It uses the existing descriptor to fuel
the necessary overrides with the exception of PropertyType,
GetValue(), and SetValue().

I override PreFilterProperties() in my webcontrol's custom
ControlDesigner looking for those properties where the PropertyType is
of Type Color. I replace those with my custom PropertyDescriptor

[VB.Net]
Friend Class MyWebColorPropertyDescriptor
Inherits System.ComponentModel.PropertyDescriptor

Private m_basePd As System.ComponentModel.PropertyDescriptor

Friend Sub New(ByVal basePd As
System.ComponentModel.PropertyDescriptor)
MyBase.New(basePd.Name, New Attribute() {New
EditorAttribute(GetType(MyWebColorEditor), GetType(UITypeEditor))})
Me.m_basePd = basePd
End Sub

Friend Shared Function CreateProperty(ByVal basePd As
System.ComponentModel.PropertyDescriptor) As
MyWebColorPropertyDescriptor
Return New MyWebColorPropertyDescriptor(basePd)
End Function



Public Overrides ReadOnly Property PropertyType() As System.Type
Get
Return GetType(String)
End Get
End Property

Public Overrides Sub SetValue(ByVal component As Object, ByVal
value As Object)
If value Is Nothing Then
Me.m_basePd.SetValue(component, value)
Else
Dim converter As TypeConverter = Me.m_basePd.Converter
Dim objValue As Object
If Not converter Is Nothing Then
objValue = converter.ConvertFrom(CStr(value))
Else
If value.GetType.IsAssignableFrom(GetType(String)) Then
objValue = ColorTranslator.FromHtml(CStr(value))
ElseIf TypeOf value Is Color Then
objValue = value
End If
End If
Me.m_basePd.SetValue(component, objValue)
End If

End Sub

Public Overrides Function GetValue(ByVal component As Object) As
Object
Dim converter As TypeConverter = Me.m_basePd.Converter
Dim objValue As Object = Me.m_basePd.GetValue(component)
Dim strValue As String
If Not converter Is Nothing Then
strValue = converter.ConvertTo(objValue, GetType(String))
Else
If Not objValue Is Nothing Then
If TypeOf objValue Is Color Then
strValue = ColorTranslator.ToHtml(CType(objValue, Color))
Else
strValue = objValue.ToString()
End If
End If
End If
Return strValue
End Function




'Defer to basePd
Public Overrides Function ShouldSerializeValue(ByVal component As
Object) As Boolean
Return Me.m_basePd.ShouldSerializeValue(component)
End Function

'...
End Class




Public Class MyControlDesigner
Inherits ControlDesigner

...

Protected Overrides Sub PreFilterProperties(ByVal properties As
System.Collections.IDictionary)
MyBase.PreFilterProperties(properties)
Dim prop As PropertyDescriptor

Dim strKey As String
Dim arrKeys As New ArrayList()
'Collect the keys where the property is a color
For Each strKey In properties.Keys
prop = properties(strKey)
If prop.PropertyType.Equals(GetType(Color)) Then
arrKeys.Add(strKey)
End If
Next

For Each strKey In arrKeys
'Replace ALL color properties with my webcolor property
descriptor
'I could do this selectively, but I like this better.
prop = properties(strKey)
prop = MyWebColorPropertyDescriptor.CreateProperty(prop)
properties.Item(strKey) = prop
Next

End Sub
End Class
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top