C # .NET Winforms: is it possible to override Label.Autosize?

I don't like the AutoSize property of the Label control. I have a custom shortcut that draws a fancy rounded border by the way. I put AutoSize = false in my constructor, however, when I put it in design mode, the property is always True.

I have successfully changed other properties, but this one happily ignores me. Does anyone have a clue if this is "by MS design"?

Here is the full source code of my tag in case anyone is interested.

 using System; using System.ComponentModel; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace Dentactil.UI.WinControls { [DefaultProperty("TextString")] [DefaultEvent("TextClick")] public partial class RoundedLabel : UserControl { private static readonly Color DEFAULT_BORDER_COLOR = Color.FromArgb( 132, 100, 161 ); private const float DEFAULT_BORDER_WIDTH = 2.0F; private const int DEFAULT_ROUNDED_WIDTH = 16; private const int DEFAULT_ROUNDED_HEIGHT = 12; private Color mBorderColor = DEFAULT_BORDER_COLOR; private float mBorderWidth = DEFAULT_BORDER_WIDTH; private int mRoundedWidth = DEFAULT_ROUNDED_WIDTH; private int mRoundedHeight = DEFAULT_ROUNDED_HEIGHT; public event EventHandler TextClick; private Padding mPadding = new Padding(8); public RoundedLabel() { InitializeComponent(); } public Cursor TextCursor { get { return lblText.Cursor; } set { lblText.Cursor = value; } } public Padding TextPadding { get { return mPadding; } set { mPadding = value; UpdateInternalBounds(); } } public ContentAlignment TextAlign { get { return lblText.TextAlign; } set { lblText.TextAlign = value; } } public string TextString { get { return lblText.Text; } set { lblText.Text = value; } } public override Font Font { get { return base.Font; } set { base.Font = value; lblText.Font = value; } } public override Color ForeColor { get { return base.ForeColor; } set { base.ForeColor = value; lblText.ForeColor = value; } } public Color BorderColor { get { return mBorderColor; } set { mBorderColor = value; Invalidate(); } } [DefaultValue(DEFAULT_BORDER_WIDTH)] public float BorderWidth { get { return mBorderWidth; } set { mBorderWidth = value; Invalidate(); } } [DefaultValue(DEFAULT_ROUNDED_WIDTH)] public int RoundedWidth { get { return mRoundedWidth; } set { mRoundedWidth = value; Invalidate(); } } [DefaultValue(DEFAULT_ROUNDED_HEIGHT)] public int RoundedHeight { get { return mRoundedHeight; } set { mRoundedHeight = value; Invalidate(); } } private void UpdateInternalBounds() { lblText.Left = mPadding.Left; lblText.Top = mPadding.Top; int width = Width - mPadding.Right - mPadding.Left; lblText.Width = width > 0 ? width : 0; int heigth = Height - mPadding.Bottom - mPadding.Top; lblText.Height = heigth > 0 ? heigth : 0; } protected override void OnLoad(EventArgs e) { UpdateInternalBounds(); base.OnLoad(e); } protected override void OnPaint(PaintEventArgs e) { SmoothingMode smoothingMode = e.Graphics.SmoothingMode; e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; int roundedWidth = RoundedWidth > (Width - 1)/2 ? (Width - 1)/2 : RoundedWidth; int roundedHeight = RoundedHeight > (Height - 1)/2 ? (Height - 1)/2 : RoundedHeight; GraphicsPath path = new GraphicsPath(); path.AddLine(0, roundedHeight, 0, Height - 1 - roundedHeight); path.AddArc(new RectangleF(0, Height - 1 - 2*roundedHeight, 2*roundedWidth, 2*roundedHeight), 180, -90); path.AddLine(roundedWidth, Height - 1, Width - 1 - 2*roundedWidth, Height - 1); path.AddArc(new RectangleF(Width - 1 - 2*roundedWidth, Height - 1 - 2*roundedHeight, 2*roundedWidth, 2*roundedHeight), 90, -90); path.AddLine(Width - 1, Height - 1 - roundedHeight, Width - 1, roundedHeight); path.AddArc(new RectangleF(Width - 1 - 2*roundedWidth, 0, 2*roundedWidth, 2*roundedHeight), 0, -90); path.AddLine(Width - 1 - roundedWidth, 0, roundedWidth, 0); path.AddArc(new RectangleF(0, 0, 2*roundedWidth, 2*roundedHeight), -90, -90); e.Graphics.DrawPath(new Pen(new SolidBrush(BorderColor), BorderWidth), path); e.Graphics.SmoothingMode = smoothingMode; base.OnPaint(e); } protected override void OnResize(EventArgs e) { UpdateInternalBounds(); base.OnResize(e); } private void lblText_Click(object sender, EventArgs e) { if (TextClick != null) { TextClick(this, e); } } } } 

(there are some problems with markup and Underscore, but it's easy to follow the code).


I really deleted this override some time ago when I saw that it was not working. I will add it again and test. Basically I want to replace Label with a new shortcut: IWillNotAutoSizeLabel;)

Basically, I hate the default autosize property.

+4
source share
4 answers

I saw a similar behavior when setting certain properties of controls in the constructor of the form itself. They seem to be back to their defaults for design.

I noticed that you are already overriding the OnLoad method. Have you tried setting AutoSize = false? Or are you mostly worried about providing a default value of false?

+3
source

I spent a lot of time with him, and it finally works! (my code is vb.net, but it's easy to convert)

 Private _Autosize As Boolean Public Sub New() _Autosize=False End Sub Public Overrides Property AutoSize() As Boolean Get Return MyBase.AutoSize End Get Set(ByVal Value As Boolean) If _Autosize <> Value And _Autosize = False Then MyBase.AutoSize = False _Autosize = Value Else MyBase.AutoSize = Value End If End Set End Property 
+2
source

Your problem may be that you are not actually overriding Autosize in your code (i.e., in the same way that you overriding Font or ForeColor).

+1
source

I do not see this.AutoSize = false in your constructor. Your class is marked as partial - perhaps you have a constructor in another file with this line. The visual studio designer will name this bottomless designer that you have.

0
source

Source: https://habr.com/ru/post/1276422/


All Articles