matthew_b
matthew_b

Reputation: 759

Setting default size/text of custom control in c#

I am creating a custom control in my C# application in order to add a new property (MyProperty below). It is inheriting from Label. One thing I would like it to do, is display at a particular size when I drag it on to my form (200x132). I'd also like it to display no text. However, no matter how I try to do this, it doesn't seem to work. I am able to set BackColor and BorderStyle with no problem, however. I'm fairly new to C#, so maybe I'm missing something obvious.

Here is my code:

using System.Drawing;
using System.Windows.Forms;

namespace MyProgram
{

    public enum MyEnum
    {
        Value1, Value2, Value3
    }

    public partial class MyControl : Label
    {

        public MyControl()
        {
            BackColor = Color.LightCoral;
            BorderStyle = BorderStyle.FixedSingle;
            AutoSize = false;
            Size = new Size(200, 132);
            Text = "";
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

        private MyEnum myProperty;

        public MyEnum MyProperty
        {
            get { return myProperty; }
            set { myPropery = value; }
        }
    }
}

Upvotes: 3

Views: 2218

Answers (2)

cramopy
cramopy

Reputation: 3497

@Dispersia reply only answers the myControl1 thing. (deleted meanwhile)

Here comes a full guide for solving your problem:

  • Add a new UserControl named MyLabel
  • Change the following within Designer Mode:
    • BorderStyle:= FixedSingle
    • Size:= 200; 132
  • Now Drag&Drop a new Label onto the control
  • Edit those Label values (also within Designer Mode):
    • AutoSize:= false
    • BackColor:= LightCoral
    • Dock:= Fill
    • Text:= clear/empty this box!! (don't write this inside the box, you really have to clear it!)
    • TextAlign:= MiddleCenter

Just recompile your project && add a MyLabel control from the Toolbar.
Now it show up as you wanted!!

Upvotes: 1

test
test

Reputation: 2639

The answer provided via Dispersia's link has a bug, in my opinion. The text reset should happen once and then whatever a user does after that shouldn't matter. In Dispersia's link you can't actually set the text back to the control name because it will keep blanking it out.

The answer provided by cramopy doesn't technically answer your question, it is a way to do it by using the defaults on a UserControl though. You'll also need to bind the Text property of the UserControl to the label's.

The following should work while inheriting from a Label and will only reset the Text property once.

public partial class MyControl : Label
{

    #region fields

    private IComponentChangeService _changeService;
    private bool canResetText = false;

    #endregion

    #region properties

    protected override Size DefaultSize
    {
        get { return new Size(200, 132); }
    }

    [Browsable(false)]
    public override bool AutoSize
    {
        get { return false; }
        set { base.AutoSize = false; }
    }

    public override ISite Site
    {
        get { return base.Site; }
        set
        {
            base.Site = value;

            if (!base.DesignMode)
                return;

            this._changeService = (IComponentChangeService)base.GetService(typeof(IComponentChangeService));

            if (this._changeService != null)
                this._changeService.ComponentChanged += new ComponentChangedEventHandler(this.OnComponentChanged);
        }
    }

    #endregion

    #region constructors

    public MyControl()
    {
        base.BackColor = Color.LightCoral;
        base.BorderStyle = BorderStyle.FixedSingle;
    }

    #endregion

    #region methods

    protected override void InitLayout()
    {
        base.InitLayout();

        this.canResetText = true;
    }

    private void OnComponentChanged(object sender, ComponentChangedEventArgs ce)
    {
        if (ce.Component != null &&
            ce.Component == this &&
            ce.Member.Name == "Text" &&
            base.DesignMode &&
            this.canResetText)
        {
            ((MyControl)ce.Component).Text = string.Empty;

            this.canResetText = false;

            if (this._changeService != null)
                this._changeService.ComponentChanged -= new ComponentChangedEventHandler(this.OnComponentChanged);
        }
    }

    #endregion

}

Upvotes: 3

Related Questions