LairdPleng
LairdPleng

Reputation: 974

.NET Designer issues shadowing properties as readonly in inherited controls

I am trying to create custom controls to provide some consistency to the design of an application. However when I shadow a property with a readonly alternatvie, I get designer errors at build time. Now I can delete the offending lines of code in the designer file and continue to build and run my application but firstly this is irritating, and secondly it tells me that I must be doing something fundamentally wrong!

Here's an example of a control that overrides 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

In the first build after adding one of these controls to a form, or after changing any properties, the following lines get added to the designer file by Visual Studio:

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

Which results in the following build errors

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

As I say... I can remove the lines that Visual Stuido added, and continue, but where have I gone wrong to be getting this problem?

Upvotes: 2

Views: 778

Answers (1)

LarsTech
LarsTech

Reputation: 81665

Try telling the forms designer to not serialize those properties:

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

Make sure to rebuild your solution.

Upvotes: 2

Related Questions