Vb.net 属性默认值不适用于新控件

Vb.net 属性默认值不适用于新控件,vb.net,Vb.net,我有一个自定义的用户控件,可以扩展和折叠。我必须有第二个“开放宽度”属性,该属性必须与正常宽度属性分开设置 当我折叠控件时,它使宽度为10。当我展开控件时,它会将控件宽度返回到“open width”属性,创建控件时必须手动将该属性设置为正常宽度 控件上的默认新宽度为200,因此为了保持一致性,我想将默认的“打开宽度”属性也设置为200。因此,我有以下几点: Private _mSideBarOpenWidth As Integer <EditorBrowsable(EditorBrows

我有一个自定义的用户控件,可以扩展和折叠。我必须有第二个“开放宽度”属性,该属性必须与正常宽度属性分开设置

当我折叠控件时,它使宽度为10。当我展开控件时,它会将控件宽度返回到“open width”属性,创建控件时必须手动将该属性设置为正常宽度

控件上的默认新宽度为200,因此为了保持一致性,我想将默认的“打开宽度”属性也设置为200。因此,我有以下几点:

Private _mSideBarOpenWidth As Integer
<EditorBrowsable(EditorBrowsableState.Always)>
<Browsable(True)>
<DesignerSerializationVisibility( _
DesignerSerializationVisibility.Visible)>
<DefaultValue(200)>
<Category("SideBar")>
<Description("Sets the open Width of the SideBar control.")>
Public Property SideBarOpenWidth() As Integer
    Get
        Return _mSideBarOpenWidth
    End Get
    Set(value As Integer)
        _mSideBarOpenWidth = value
    End Set
End Property
cleaning/building/rebuilding the project
closing VisualStudio and opening it back up
deleting the form and creating a new one
using the control in a new project
setting <em>DesignerSerializationVisibility.Visible</em> to <em>.Content</em>
using <em>"200"</em> with the quotes as the default value
Private _msidebarpenwidth作为整数
公共属性SideBarOpenWidth()为整数
得到
返回_msidebarpenwidth
结束
设置(值为整数)
_msidebarpenwidth=值
端集
端属性
将新控件拖到窗体上时,默认值始终为0。如果我更改它,该值确实会保持不变,它不会从200开始。我在这个问题上做了很多搜索,并尝试了以下方法:

Private _mSideBarOpenWidth As Integer
<EditorBrowsable(EditorBrowsableState.Always)>
<Browsable(True)>
<DesignerSerializationVisibility( _
DesignerSerializationVisibility.Visible)>
<DefaultValue(200)>
<Category("SideBar")>
<Description("Sets the open Width of the SideBar control.")>
Public Property SideBarOpenWidth() As Integer
    Get
        Return _mSideBarOpenWidth
    End Get
    Set(value As Integer)
        _mSideBarOpenWidth = value
    End Set
End Property
cleaning/building/rebuilding the project
closing VisualStudio and opening it back up
deleting the form and creating a new one
using the control in a new project
setting <em>DesignerSerializationVisibility.Visible</em> to <em>.Content</em>
using <em>"200"</em> with the quotes as the default value
清理/建设/重建项目 关闭VisualStudio并将其打开备份 删除表单并创建新表单 在新项目中使用控件 设置DesignerSerializationVisibility.Visible对.Content可见 使用带引号的“200”作为默认值 以及所有这些的各种组合。这一切都不起作用,拖到窗体上的新控件的默认值变为零。不用说,我不明白为什么在创建默认值时不会将其设置为200

我唯一一次访问该属性是在设置控件的宽度时。width=SideBarOpenWidth

它不会从200开始

这不是
DefaultValue
所做的。VS使用
DefaultValue
来确定当前值是否与默认值不同,因此应序列化,并在属性IDE中以粗体显示属性值。它不设置值。来自以下方面的评论:

DefaultValueAttribute不会导致使用该属性的值自动初始化成员。必须在代码中设置初始值

Attribute
s提供有关类、属性等的信息。您的属性不知道
DefaultValue
属性,如果没有您添加的代码,它们不会交互


相反,它们将有关类或属性(等)的信息指定给其他对象(设计器、序列化程序等)。例如,要使用哪个
编辑器
类型转换器
。一个很好的例子是
Description
Category
属性-这些属性提供了有关您的属性的信息,它在IDE的属性窗格中使用这些属性。

现在我觉得自己像个白痴,因为我知道我应该先设置默认值。。。再次感谢你!给定名称,它看起来确实应该设置它,但因为它是一个属性,更不用说设置了。