Solved

Surely an Expert must know, why dont properties of custom types get code generated for them?

Posted on 2003-10-30
5
333 Views
Last Modified: 2012-06-21
So I made an object that contains a couple properties.
I include this object in a custom control.
In design mode I can edit these properties but the designer refuses to save these properties.

Why oh why does the designer refuse to write code for these properties?


Here is all the code for a test case.  Just drop the 'ColoredPane' control on to a form and change it's background style.  No code will be written to keep this property and I cant figure out why.




Imports System
Imports System.ComponentModel

Namespace Testing

#Region " ColoredPane Control "
#Region " class ColoredPane "
    <Designer(GetType(ColoredPaneDesigner))> _
    Public Class ColoredPane
        Inherits System.Windows.Forms.UserControl
#Region " Windows Form Designer generated code "
        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call
        End Sub

        'UserControl overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            components = New System.ComponentModel.Container
        End Sub
#End Region
        Private WithEvents mBackgroundStyle As New SolidBrushStyle
#Region "   property BackgroundStyle "
        <Bindable(True), Browsable(True), _
            Category("Appearance"), DefaultValue("Color.Chartreuse")> _
        Public Property BackgroundStyle() As SolidBrushStyle
            Get
                Return Me.mBackgroundStyle
            End Get
            Set(ByVal Value As SolidBrushStyle)
                Me.mBackgroundStyle = Value
                Invalidate()
            End Set
        End Property
#End Region
#Region " OnPaintBackground "
        Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)
            Dim gr As Graphics = pevent.Graphics
            Dim br As Brush
            br = Me.BackgroundStyle.CreateBrush(Me.DisplayRectangle)
            gr.FillRectangle(br, Me.DisplayRectangle)
            br.Dispose()
        End Sub
#End Region
#Region " OnBackgroundStyleChanged "
        Private Sub OnBackgroundStyleChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
                Handles mBackgroundStyle.StyleChanged
            Invalidate()
        End Sub
#End Region
    End Class
#End Region
#Region " class ColoredPaneDesigner "
    Public Class ColoredPaneDesigner
        Inherits System.Windows.Forms.Design.ControlDesigner
#Region " Constructor "
        Public Sub New()
        End Sub
#End Region
#Region " PreFilterProperties "
        Protected Overloads Overrides Sub PreFilterProperties(ByVal properties As System.Collections.IDictionary)
            properties.Remove("BackColor")
        End Sub
#End Region
    End Class
#End Region
#End Region
#Region " Object Styling "
#Region " Object Style Base Classes "
#Region " base StyleBase "
    ' StyleBase should be inherited by all style objects.
    ' It provides the StyleChanged event which should be raised
    ' any time a property of a style changes.
    <TypeConverter(GetType(ExpandableObjectConverter))> _
    Public MustInherit Class StyleBase
#Region "   event StyleChanged "
        Public Event StyleChanged(ByVal sender As Object, ByVal e As System.EventArgs)
#End Region
#Region " OnStyleChanged "
        Protected Overridable Sub OnStyleChanged(ByVal e As System.EventArgs)
            RaiseEvent StyleChanged(Me, e)
        End Sub
#End Region
    End Class
#End Region
#Region " base BrushStyleBase "
    ' BrushStyleBase extends the StyleBase by providing an interface
    ' for creating brush objects.  All object styles that represent
    ' a brush should inherit from this class and override the CreateBrush
    ' method.
    Public MustInherit Class BrushStyleBase
        Inherits StyleBase

#Region " mustoverride CreateBrush "
        Public MustOverride Function CreateBrush(ByVal bounds As Rectangle) As Brush
#End Region
    End Class
#End Region
#End Region
#Region " SolidBrush Object Style "
#Region " class SolidBrushStyle "
    ' Represents a brush with a single solid color.
    <TypeConverter(GetType(SolidBrushStyleTypeConverter))> _
    Public Class SolidBrushStyle
        Inherits BrushStyleBase

        Private mColor As Color = Color.Chartreuse
#Region " Constructors "
        Public Sub New()
            MyBase.New()
            Me.mColor = Color.Chartreuse
        End Sub
        Public Sub New(ByVal solidColor As Color)
            MyBase.New()
            Me.mColor = solidColor
        End Sub
#End Region
#Region "   property Color "
        <DefaultValue("Color.Chartreuse"), Description("A single solid color."), _
            Bindable(True), Browsable(True), MergableProperty(True), _
            NotifyParentProperty(True)> _
        Public Property Color() As Color
            Get
                Return Me.mColor
            End Get
            Set(ByVal Value As Color)
                Me.mColor = Value
                OnStyleChanged(New System.EventArgs)
            End Set
        End Property
#End Region
#Region " CreateBrush "
        Public Overrides Function CreateBrush(ByVal bounds As Rectangle) As Brush
            Dim sb As SolidBrush
            sb = New SolidBrush(Me.mColor)
            Return sb
        End Function
#End Region
#Region " ToString "
        Public Overrides Function ToString() As String
            Dim myConverter As TypeConverter
            myConverter = TypeDescriptor.GetConverter(GetType(SolidBrushStyle))
            Return myConverter.ConvertToString(Me)
        End Function
#End Region
    End Class
#End Region
#Region " class SolidBrushStyleTypeConverter "
    Class SolidBrushStyleTypeConverter
        Inherits ExpandableObjectConverter
#Region " CanConvertFrom "
        Public Overloads Overrides Function CanConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal sourceType As System.Type) As Boolean
            If sourceType Is GetType(String) Then
                Return True
            End If
            Return False
        End Function
#End Region
#Region " CanConvertTo "
        Public Overloads Overrides Function CanConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As System.Type) As Boolean
            If destinationType Is GetType(String) Then
                Return True
            End If
            Return False
        End Function
#End Region
#Region " ConvertFrom "
        Public Overloads Overrides Function ConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object) As Object
            If GetType(String).IsInstanceOfType(value) Then
                Dim sbs As New SolidBrushStyle
                Dim colorConverter As TypeConverter
                colorConverter = TypeDescriptor.GetConverter(GetType(Color))
                sbs.Color = colorConverter.ConvertFromString(CType(value, String))
                Return sbs
            End If
            Return Nothing
        End Function
#End Region
#Region " ConvertTo "
        Public Overloads Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As System.Type) As Object
            Dim sbs As SolidBrushStyle = CType(value, SolidBrushStyle)
            If destinationType Is GetType(String) Then
                Dim colorConverter As TypeConverter
                colorConverter = TypeDescriptor.GetConverter(GetType(Color))
                Return colorConverter.ConvertTo(sbs.Color, GetType(String))
            End If
            Return Nothing
        End Function
#End Region
#Region " GetPropertiesSupported "
        Public Overloads Overrides Function GetPropertiesSupported(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean
            Return True
        End Function
#End Region
#Region " GetProperties "
        Public Overloads Overrides Function GetProperties(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal value As Object, ByVal attributes() As System.Attribute) As System.ComponentModel.PropertyDescriptorCollection
            Dim pdc As System.ComponentModel.PropertyDescriptorCollection
            pdc = TypeDescriptor.GetProperties(value.GetType, attributes)
            Return pdc
        End Function
#End Region
#Region " GetCreateInstanceSupported "
        Public Overloads Overrides Function GetCreateInstanceSupported(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean
            Return True
        End Function
#End Region
#Region " CreateInstance "
        Public Overloads Overrides Function CreateInstance(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal propertyValues As System.Collections.IDictionary) As Object
            Dim sbs As New SolidBrushStyle
            sbs.Color = propertyValues("Color")
            Return sbs
        End Function
#End Region
    End Class
#End Region
#End Region
#End Region

End Namespace
0
Comment
Question by:bacon7181
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 6

Expert Comment

by:zrh
ID: 9653791
Try adding the DesignerSerializationVisibilityAttribute on the property in question.
Add something like this in designer class:
            protected override void PreFilterProperties(IDictionary properties) {
                  base.PreFilterProperties(properties);

                  PropertyDescriptor prop;

                  prop = (PropertyDescriptor)properties["BackgroundStyle"];
                  Debug.Assert(prop != null);
                  prop = TypeDescriptor.CreateProperty(this.GetType(), prop,
                        new Attribute[] {
                                                      new DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Content)
                                                });
                  properties["BackgroundStyle"] = prop;
            }

Hope that helps some,
ZRH
0
 
LVL 3

Author Comment

by:bacon7181
ID: 9703411
I have still been unable to get this to work.

I am using VB.NET.  I set the DesignerSerializationVisibility attributes and they dont seem to do anything.

I've found an easier example to work with.  At least I think its the same problem.

When I inherit from a UserControl and want to provide a Text property I cant get the designer to serialize it.  I set Browsable(True) to display the Text property and I tried the serialization visibilities to make the text property save but I cant get it working.
0
 
LVL 6

Accepted Solution

by:
zrh earned 500 total points
ID: 9704260
Ok, lets try the KIS (Keep it simple) approach :)
On the BackgroundStyle property in the control class, not the designer class, add the
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
attribute.

So your BackgroundStyle property looks like:
#Region "   property BackgroundStyle "
        <Bindable(True), _
          Browsable(True), _
          Category("Appearance"), _
          DefaultValue("Color.Chartreuse"), _
          DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
        Public Property BackgroundStyle() As SolidBrushStyle
            Get
                Return Me.mBackgroundStyle
            End Get
            Set(ByVal Value As SolidBrushStyle)
                Me.mBackgroundStyle = Value
                Invalidate()
            End Set
        End Property
#End Region

And your PreFilterProperties method in the designer class looks like:
#Region " PreFilterProperties "
        Protected Overloads Overrides Sub PreFilterProperties(ByVal properties As System.Collections.IDictionary)
            MyBase.PreFilterProperties(properties)
            properties.Remove("BackColor")
        End Sub
#End Region

I tested the above, and it worked for me,
Hope that helps,
ZRH
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10232265
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: zrh {http:#9704260}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer
0

Featured Post

CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question