'How to block setting properties in autogenerated code?
My WinForms project has a special container control with functionality similar to FlowLayoutPanel. In fact, it is a set of buttons with some additional features, such as automatically changing the design depending on certain common properties.
Content is generated based on a property called Buttons, an array of structures consisting of Key, Text, HotKey, and Enabled fields.
There are also inherited classes which in the constructor of the class set the value of property Buttons and accordingly buttons, for example, SettingsBlock.
Public Class SettingsBlock
Inherits ButtonsFlow
Public Const About = "About"
Public Const SettingsChange = "SettingsChange"
Public Const TaskSelect = "TaskSelect"
Public Sub New()
Margin = New Padding()
Caption = "Налаштування"
HighlidhtColor = Color.Silver
MyBase.Buttons = {New KeyText(SettingsChange, "Змінити налаштування"), New KeyText(TaskSelect, "Обрати завдання"), New KeyText(About, "Про програму")}
End Sub
Public Overrides Property Buttons As KeyText()
Get
Return MyBase.Buttons
End Get
Set
End Set
End Property
Public Property TaskSelectorEnabled As Boolean
Get
Return Enabled(TaskSelect)
End Get
Set
Enabled(TaskSelect) = Value
End Set
End Property
End Class
The problem is that when you insert such ready-made blocks in the form the assignment is repeated in an auto-generated code.
Dim KeyText4 As WinFormsView.KeyText = New WinFormsView.KeyText()
Dim KeyText5 As WinFormsView.KeyText = New WinFormsView.KeyText()
Dim KeyText6 As WinFormsView.KeyText = New WinFormsView.KeyText()
KeyText4.Enabled = True
KeyText4.HotKey = System.Windows.Forms.Keys.None
KeyText4.Key = "SettingsChange"
KeyText4.Text = "Змінити налаштування"
KeyText5.Enabled = True
KeyText5.HotKey = System.Windows.Forms.Keys.None
KeyText5.Key = "TaskSelect"
KeyText5.Text = "Обрати завдання"
KeyText6.Enabled = True
KeyText6.HotKey = System.Windows.Forms.Keys.None
KeyText6.Key = "About"
KeyText6.Text = "Про програму"
Me.ButtonsSettings.Buttons = New WinFormsView.KeyText() {KeyText4, KeyText5, KeyText6}
In same coditions designer replaces inintial set with wrong values. I solve this problem by macking property Buttons pseudo readonly in derived classes. I know that it is autogenerated code but is it possible to prevent this redundant assignment?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
