4

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

Oskytar
  • 205
  • 2
  • 8
  • Default values in the properties tab in visual studio show as normal text and changed properties show as Bold. Somehow VS is identifying that it wasnt set by default. – CathalMF Jul 08 '15 at 15:49

1 Answers1

4

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.

Rafal
  • 12,391
  • 32
  • 54
  • 2
    And when you do so, ensure that you use a single (private) constant for both the DefaultValue() and the field initialization. Don't type the same thing in twice. :) – Matthew Watson Jan 09 '13 at 12:57
  • Thank you very much, you solved my problem. I edited the question with sample code and one more question. – Oskytar Jan 10 '13 at 16:20