Search code examples
c#default-valuecustom-properties

C# custom control property: unable to set DefaultValue field


I am trying to set a custom C# control property.

Here's my code:

/* Cancel's button text */
[Category("ComboTouch"),
Description("Text to display in cancel button"),
DefaultValue("Cancel")]
public String ct_cancelButtonText { get; set; }

I can get the property when I use the customized control in other projects (as you can see in the image); but configuration parameter DefaultValue seems not to work.

Could anybody help me? Thank you very much.

Custom Control Properties

01/10/13 Update. Thank you very much for your answers, you solved my problem.

I would like to share how I finally could set the default value automatically:

  private String m_cancelButtonText="Cancel";

    /* Cancel's button text */
    [Category("ComboTouch"),
    Description("Text to display in cancel button"),
    DefaultValue("Cancel")]
    public String ct_cancelButtonText
    {
        get
        {
            return m_cancelButtonText;
        }
        set
        {
            m_cancelButtonText = value;
        }
    }

One curiosity: please check the format of 'Cancel' text. If I set DefaultValue type; it looks like normal text. But if I don't, it looks like bold text. I know it's silly; but I would like to know why it is that way. Thank you.

enter image description here


Solution

  • As noted in documentation:

    A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. You must set the initial value in your code.