Question:
When inheriting, for example, from FlowLayoutPanel, I set properties in the constructor:
class myFlow : FlowLayoutPanel
{
public myFlow()
{
AutoSize = true;
AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
}
}
in the designer I throw this element on the form and, for example, I want to return AutoSize=false back. It seems that the element on the form changes and looks as it should, but after the rebuild and closing-opening of the form designer, the property again has the value specified in the constructor. Tried override InitLayout but same effect. How to properly configure components so that you can change them in the designer?
Answer:
[Browsable(true), EditorBrowsable(EditorBrowsableState.Always),DefaultValue(true)
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override bool AutoSize
{
get
{
return base.AutoSize;
}
set
{
base.AutoSize = value;
}
}
I spied this code in the Panel sources from which the FlowLayoutPanel inherits its AutoSize. The DefaultValue(true) parameter, which actually does what you need to add yourself … I peeped its existence and the possibility of using it in the same Panel sources
AutoSizeMode I think, by analogy, it should work … For correct operation, you need using System.ComponentModel;
Accordingly, the setting of the values of these properties must be removed from the constructor.