Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

winforms

I don't like the AutoSize property of the Label control. I have a custom Label that draws a fancy rounded border among other things. I'm placing a AutoSize = false in my constructor, however, when I place it in design mode, the property always is True.

I have overridden other properties with success but this one is happily ignoring me. Does anybody has a clue if this is "by MS design"?

Here's the full source code of my Label 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 issues with Stack Overflow's markup and the Underscore, but it's easy to follow the code).


I have actually removed that override some time ago when I saw that it wasn't working. I'll add it again now and test. Basically I want to replace the Label with some new label called: IWillNotAutoSizeLabel ;)

I basically hate the autosize property "on by default".

like image 821
Martin Marconcini Avatar asked Aug 24 '08 17:08

Martin Marconcini


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

I've seen similar behaviour when setting certain properties of controls in the constructor of the form itself. They seem to revert back to their design-time defaults.

I notice you're already overriding the OnLoad method. Have you tried setting AutoSize = false there? Or are you mainly concerned with providing a default value of false?

like image 189
Matt Hamilton Avatar answered Nov 09 '22 03:11

Matt Hamilton