自定义表单ExpandableObjectConverter不会将值保存到设计器 [英] Custom form ExpandableObjectConverter does not save values to designer

查看:168
本文介绍了自定义表单ExpandableObjectConverter不会将值保存到设计器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个将继承的自定义表单。我无法让设计人员将ExpandableObjectConverter值保存到设计器。



Hi, I am creating a custom form that will be inherited. I cannot get the designer to save the ExpandableObjectConverter values to designer.

Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports Lens.FormAppearance

''' <summary>
''' Lens metro form.
''' </summary>
''' <remarks></remarks>
<Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", GetType(IDesigner))> _
<System.Serializable()> _
<DefaultEvent("Load")> _
<DesignTimeVisible(True)> _
<Description("Lens metro form.")> _
<ToolboxItem(False)> _
<ToolboxBitmap(GetType(MetroForm), "software_vs_form1a_16x16.png")> _
Public Class MetroForm

    Private _Visible As Boolean = False

'cannot get 'NavigationButtonsAppearance' to write to designer.
    Private WithEvents NavigationButtonsAppearanceValue As New NavigationButtonsAppearance(NavigationButtonAlignment.MiddleLeft, False)
    ''' <summary>
    ''' Get or set the appearance of navigation buttons.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Description("Get or set the appearance of navigation buttons."), _
    Browsable(True), _
    RefreshProperties(RefreshProperties.All), _
    EditorBrowsable(EditorBrowsableState.Always), _
    NotifyParentProperty(True), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), _
    Category("LensMetroForm")> _
    Public Property NavigationButtons() As NavigationButtonsAppearance
        Get
            Return NavigationButtonsAppearanceValue
        End Get
        Set(ByVal value As NavigationButtonsAppearance)
            Try
                NavigationButtonsAppearanceValue = value
                NavigationButtonsAppearanceValue_PropertyChanged()
                Invalidate()
            Catch ex As Exception
                Throw New ArgumentException("NavigationButtonsAppearance Problem: " & ex.ToString)
            End Try
        End Set
    End Property

    Private Sub NavigationButtonsAppearanceValue_PropertyChanged() Handles NavigationButtonsAppearanceValue.PropertyChanged
        Try
            _Alignment = CType(NavigationButtonsAppearanceValue.Align, NavigationButtonAlignment)
            _Visible = CType(NavigationButtonsAppearanceValue.Visible, Boolean)
            Invalidate()
        Catch ex As Exception
            Console.WriteLine(ex.ToString)
        End Try
    End Sub

'this writes to designer ok.
    ''' <summary>
    ''' Gets or sets a value to indicate if navigation buttons are shown.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Description("Gets or sets a value to indicate if navigation buttons are shown."), _
    TypeConverter(GetType(YesNoTypeConverter)), _
    DefaultValue(GetType(Boolean), "False"), _
    Browsable(False), _
    RefreshProperties(RefreshProperties.All), _
    EditorBrowsable(EditorBrowsableState.Never), _
    NotifyParentProperty(True), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), _
    Category("LensMetroForm")> _
    Public Property NavBtnVisible() As Boolean
        Get
            Return _Visible
        End Get
        Set(ByVal value As Boolean)
            _Visible = value
            If Not NavigationButtonsAppearanceValue.Visible = value Then
                NavigationButtonsAppearanceValue.Visible = value
            End If
            Invalidate()
        End Set
    End Property

'only constructor for new
    Public Sub New()
        InitializeComponent()
SetStyle(ControlStyles.SupportsTransparentBackColor, True)
            SetStyle(ControlStyles.Opaque, False)
            SetStyle(ControlStyles.DoubleBuffer, True)
            SetStyle(ControlStyles.AllPaintingInWmPaint, True)
            SetStyle(ControlStyles.UserPaint, True)
            SetStyle(ControlStyles.UserMouse, True)
            SetStyle(ControlStyles.StandardClick, True)
            SetStyle(ControlStyles.Selectable, True)
            SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
            SetStyle(ControlStyles.ResizeRedraw, True)
            SetStyle(ControlStyles.CacheText, False)
            SetStyle(ControlStyles.EnableNotifyMessage, True)
            SetStyle(ControlStyles.FixedHeight, False)
            SetStyle(ControlStyles.FixedWidth, False)
            UpdateStyles()
        NavigationButtonsAppearanceValue_PropertyChanged()
    End Sub

End Class

'code for 'NavigationButtonsAppearance' ExpandableObjectConverter
Namespace FormAppearance

    <System.Serializable(), _
    TypeConverter(GetType(NavigationButtonsAppearanceConverter))> _
    Public Class NavigationButtonsAppearance
        Event PropertyChanged()

        Public Sub New(ByVal nAlignment As NavigationButtonAlignment, _
                            ByVal nShowNavigationButtons As Boolean)
            Try
                Align = nAlignment
                Visible = nShowNavigationButtons
                RaiseEvent PropertyChanged()
            Catch ex As Exception
                Console.WriteLine(ex.ToString)
            End Try
        End Sub

        Private _Alignment As NavigationButtonAlignment = NavigationButtonAlignment.MiddleLeft
        ''' <summary>
        ''' Gets or sets the form navigation buttons alignment.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Description("Gets or sets the form navigation buttons alignment."), _
        DefaultValue(GetType(NavigationButtonAlignment), "MiddleLeft"), _
        Browsable(True), _
        RefreshProperties(RefreshProperties.All), _
        EditorBrowsable(EditorBrowsableState.Always), _
        NotifyParentProperty(True), _
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _
        Category("Navigation")> _
        Public Property Align() As NavigationButtonAlignment
            Get
                Return _Alignment
            End Get
            Set(ByVal value As NavigationButtonAlignment)
                _Alignment = value
                RaiseEvent PropertyChanged()
            End Set
        End Property

        Private _Visible As Boolean = False
        ''' <summary>
        ''' Gets or sets a value to indicate if navigation buttons are shown.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Description("Gets or sets a value to indicate if navigation buttons are shown."), _
        TypeConverter(GetType(YesNoTypeConverter)), _
        DefaultValue(GetType(Boolean), "False"), _
        Browsable(True), _
        RefreshProperties(RefreshProperties.All), _
        EditorBrowsable(EditorBrowsableState.Always), _
        NotifyParentProperty(True), _
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _
        Category("Navigation")> _
        Public Property Visible() As Boolean
            Get
                Return _Visible
            End Get
            Set(ByVal value As Boolean)
                _Visible = value
                RaiseEvent PropertyChanged()
            End Set
        End Property

    End Class

End Namespace

Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization
Imports System.Globalization
Imports System.Reflection
Imports Lens.FormAppearance

<System.Serializable(), _
System.Diagnostics.DebuggerStepThrough()> _
Public Class NavigationButtonsAppearanceConverter
    Inherits ExpandableObjectConverter

    Public Overloads Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal sourceType As Type) As Boolean
        If sourceType Is GetType(String) Then
            Return False
        Else
            Return MyBase.CanConvertFrom(context, sourceType)
        End If
    End Function
    Public Overloads Overrides Function CanConvertTo(ByVal context As ITypeDescriptorContext, ByVal destinationType As Type) As Boolean
        If destinationType Is GetType(InstanceDescriptor) Or destinationType Is GetType(String) Then
            Return True
        End If
        Return MyBase.CanConvertTo(context, destinationType)
    End Function
    Public Overloads Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object) As Object

        Dim __Value As String = String.Empty
        Try
            __Value = CStr(value)
            If Not (__Value Is Nothing And __Value.Length <> 0) Then
                __Value = __Value.Trim()
                If culture Is Nothing Then culture = CultureInfo.CurrentCulture
                Dim params As String() = __Value.Split(",".ToCharArray())

                Dim _Alignment As NavigationButtonAlignment
                Dim _UseNavigationButtons As Boolean

                If IsNothing(params(0)) OrElse params(0) = "" Then _Alignment = 0 Else _Alignment = CType(params(0), NavigationButtonAlignment)
                If IsNothing(params(1)) OrElse params(1) = "" Then _UseNavigationButtons = True Else _UseNavigationButtons = CType(params(1), Boolean)

                Return New NavigationButtonsAppearance(_Alignment, _
                _UseNavigationButtons)

            Else
                'Return String.Empty
                Return MyBase.ConvertFrom(context, culture, value)
            End If
        Catch ex As Exception
            Throw New ArgumentException("ConvertFrom: = " & __Value.ToString & "= " & ex.ToString)
        End Try

    End Function

    Public Overloads Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As Type) As Object

        Dim __NavigationButtonsAppearance As NavigationButtonsAppearance
        Try
            __NavigationButtonsAppearance = CType(value, NavigationButtonsAppearance)
            If destinationType Is GetType(InstanceDescriptor) Then
                Dim argTypes As Type() = New Type(1) {}
                argTypes(0) = GetType(NavigationButtonAlignment)
                argTypes(1) = GetType(Boolean)
                Dim constructor As ConstructorInfo = GetType(NavigationButtonsAppearance).GetConstructor(argTypes)
                Dim arguments As Object() = New Object(1) {}
                arguments(0) = __NavigationButtonsAppearance.Align
                arguments(1) = __NavigationButtonsAppearance.Visible
                Return New InstanceDescriptor(constructor, arguments)
            ElseIf (destinationType Is GetType(String)) Then
                If (culture Is Nothing) Then culture = CultureInfo.CurrentCulture
                Dim Params(1) As String
                Params(0) = CStr(__NavigationButtonsAppearance.Align)
                Params(1) = CStr(__NavigationButtonsAppearance.Visible)
                'Return Params(0) & ", " & Params(1)
                Return String.Empty
            Else
                Return MyBase.ConvertTo(context, culture, value, destinationType)
            End If
        Catch ex As Exception
            Throw New ArgumentException("Cannot ConvertTo: " & ex.ToString)
        End Try

    End Function

End Class





Any help will be appreciated. Thank you in advance.



Any help will be appreciated. Thank you in advance.

推荐答案

Next time it would be better to write a reply to my comment - so I get notified ... ;)



You should change only some of your Property-Attributes.

At the Property where you use the class in your Form change the following :



Next time it would be better to write a reply to my comment - so I get notified ... ;)

You should change only some of your Property-Attributes.
At the Property where you use the class in your Form change the following :

<Description("Get or set the appearance of navigation buttons."), _
 RefreshProperties(RefreshProperties.All), _
 DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _
 Category("LensMetroForm")> _
 Public Property NavigationButtons As NavigationButtonsAppearance





Inside your class-definition you should remove the same Attribute from each Property.



Normally you don’t need most of the Attributes inside the Class-Definition.

It’s enough like this :



Inside your class-definition you should remove the same Attribute from each Property.

Normally you don't need most of the Attributes inside the Class-Definition.
It's enough like this :

<Description("Gets or sets a value to indicate if navigation buttons are shown."), _
 DefaultValue(GetType(Boolean), "False"), _
 RefreshProperties(RefreshProperties.All), _
 Category("Navigation")> _
 Public Property Visible() As Boolean





The RefreshProperties-Attribute is also only nessary if a change at this Property should actualize the Property-Grid because you changed also another Property ...



The RefreshProperties-Attribute is also only nessary if a change at this Property should actualize the Property-Grid because you changed also another Property ...


这篇关于自定义表单ExpandableObjectConverter不会将值保存到设计器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆