2

我正在尝试创建自定义控件来为应用程序的设计提供一些一致性。但是,当我使用只读的替代方法隐藏属性时,我会在构建时遇到设计器错误。现在我可以删除设计器文件中有问题的代码行并继续构建和运行我的应用程序,但首先这很烦人,其次它告诉我我一定做错了什么!

这是一个覆盖 datagridview 的控件示例

Class standardDataGridView
Inherits DataGridView

Public Sub New()
    MyBase.New()
    Me.RowHeadersVisible = False
    MyBase.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    MyBase.MultiSelect = False
    Me.ReadOnly = True
    Me.BackgroundColor = Color.White
    Me.AllowUserToDeleteRows = False
    Me.AllowUserToResizeRows = False
    Me.AllowUserToAddRows = False
End Sub

Public Shadows ReadOnly Property SelectionMode As DataGridViewSelectionMode
    Get
        Return MyBase.SelectionMode
    End Get
End Property

Public Shadows ReadOnly Property MultiSelect As Boolean
    Get
        Return MyBase.MultiSelect
    End Get
End Property

End Class

在将其中一个控件添加到窗体后或更改任何属性后的第一次构建中,Visual Studio 会将以下几行添加到设计器文件中:

Me.standardDataGridView1.MultiSelect = False
Me.standardDataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect

这会导致以下构建错误

Property 'MultiSelect' is 'ReadOnly'.
Property 'SelectionMode' is 'ReadOnly'

正如我所说的......我可以删除 Visual Stuido 添加的行,然后继续,但是我哪里出了问题来得到这个问题?

4

1 回答 1

2

尝试告诉表单设计器不要序列化这些属性:

Imports System.ComponentModel

<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
Public Shadows ReadOnly Property SelectionMode As DataGridViewSelectionMode
  Get
    Return MyBase.SelectionMode
  End Get
End Property

<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
Public Shadows ReadOnly Property MultiSelect As Boolean
  Get
    Return MyBase.MultiSelect
  End Get
End Property

确保重建您的解决方案。

于 2013-04-24T14:56:53.320 回答