* .Designer.vb无法正确创建自定义属性

时间:2016-07-25 09:47:29

标签: vb.net visual-studio visual-studio-2013

在设计模式下编辑时,VS中的控件会在designer.vb文件中发生更改,即:在按钮中,设置宽度200,高度100为Size属性,在其表单的designer.vs文件中出现:

me.button.Size = new System.Drawing.Size(200,100) 'The standard constructor

我想在我的自定义类中使用相同的内容,但我只能得到:

me.custombutton.Situation.Tab = 0
me.custombutton.Situation.Row = 0
me.custombutton.Situation.Column = 0

而不是

me.custombutton.Situation = new Situation(0,0,0) 'As my constructor works.

此外,我认为此错误会对init程序生成null异常...

我的自定义类:

 <TypeConverter(GetType(SituationConverter)), _
Description("Situation in a {tab, row, column} system"), _
Browsable(True), _
Serializable()> _
Public Class Situation
    Private _tab As Integer
    Private _row As Integer
    Private _column As Integer

    <DefaultValue(0), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)>
    Public Property Tab As Integer
        Get
            Return _tab
        End Get
        Set(value As Integer)
            _tab = value
        End Set
    End Property

    <DefaultValue(0), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)>
    Public Property Row As Integer
        Get
            Return _row
        End Get
        Set(value As Integer)
            _row = value
        End Set
    End Property

    <DefaultValue(0), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)>
    Public Property Column As Integer
        Get
            Return _column
        End Get
        Set(value As Integer)
            _column = value
        End Set
    End Property

    Public Sub New(aTab As Integer, aRow As Integer, aColumn As Integer)
        Tab = aTab
        Row = aRow
        Column = aColumn
    End Sub
End Class

Public Class SituationConverter
    Inherits ExpandableObjectConverter

    ' tells the IDE what conversions it can handle:
    Public Overloads Overrides Function CanConvertTo(context As ITypeDescriptorContext,
                                              destType As Type) As Boolean
        If destType = GetType(Situation) Then
            ' Yes I Can
            Return True
        End If
        ' Probably have to also say YES to an InstanceDescriptor 
        Return MyBase.CanConvertTo(context, destType)
    End Function


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

        If destType = GetType(String) AndAlso TypeOf value Is Situation Then

            Dim sit As Situation = CType(value, Situation)
            Return String.Format("{0}, {1}, {2}",
                    sit.Tab.ToString,
                    sit.Row.ToString,
                    sit.Column.ToString)

            ' outputs: X, Y, <ex>
            ' where Ex is what we use in the ExtraItemConverter

        End If

        Return MyBase.ConvertTo(context, culture, value, destType)
    End Function

    Public Overloads Overrides Function ConvertFrom(context As ITypeDescriptorContext,
                                    culture As Globalization.CultureInfo,
                                    value As Object) As Object

        If TypeOf value Is String Then
            Try
                ' parse the string you made and create a Foo
                Dim els As String() = value.Split(","c)
                Return New Situation(Convert.ToInt32(els(0)), Convert.ToInt32(els(1)), Convert.ToInt32(els(2)))
            Catch
                Throw New ArgumentException("Can not convert '" & CStr(value) & _
                                            "' to type Situation")
            End Try
        End If



    End Function

    Public Overrides Function CanConvertFrom(context As ITypeDescriptorContext, sourceType As Type) As Boolean
        If sourceType = GetType(String) Then
            ' Yes I Can
            Return True
        End If
        Return MyBase.CanConvertFrom(context, sourceType)
    End Function

End Class

在我的自定义按钮中。自定义属性:

       Dim _situation as New Situation(0,0,0) 'This resolved compile errors!!

    <Description("Situation in the {tab, row, column} system"),
           EditorBrowsable(EditorBrowsableState.Always), _
   DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
        Public Property Situation() As Situation
            Get
                Return _situation
            End Get
            Set(value As Situation)
                _situation = value
            End Set
        End Property

        'By answer of Visual Vincent
        Private Function ShouldSerializeSituation() As Boolean
            Return True
        End Function

        Private Sub ResetSituation()
            Dim s As Situation = New Situation(0, 0, 0)
            Situation = s
        End Sub
        '

另外,我在Desgin模式下将属性值设置为字符串,即“1,2,1”,但是当编译和运行值设置为空时...

编辑: 我添加了@Visual Vincent推荐的代码,现在得到以下内容:

Me.btnCustom1.Situation.Column = 1
    Me.btnCustom1.Situation.Row = 1
    Me.btnCustom1.Situation.Tab = 0

但是在运行时我收到以下错误:

  

System.InvalidOperationException:创建表单时出错。   有关详细信息,请参阅Exception.InnerException。其中的错误是:对象   引用未设置为对象的实例。

我的班级应该是相关的......

0 个答案:

没有答案