我给控件添加属性的时候DefaultValue("abc")已经设置了,可是为什么编译完后初始值不是"abc"???
请教各位。
代码如下:
[Category("Appearance"), Description("文本框显示的内容"),DefaultValue("abc")]        public override string Text
        {
            get
            {
                return txtDate.Text;
            }
            set
            {
                txtDate.Text = value;
            }
        }

解决方案 »

  1.   

    Text的是空的,我想给它初始值,我应该怎么做呢??
      

  2.   

    可以这样获取默认值: if (string.IsNullOrEmpty(this.txtDate.Text))
    {
    // Gets the attributes for the property.
    object attr = TypeDescriptor.GetProperties(this)["Text"].Attributes[typeof(DefaultValueAttribute)];
    DefaultValueAttribute defAttr = attr as DefaultValueAttribute;
    if (defAttr != null)
    {
    return defAttr.Value as string;
    }
    }
    return this.txtDate.Text;
      

  3.   

    DefaultValue是给PropertyGrid看的,当值等于DefaultValue时,PropertyGrid就不会对这个值加粗,要设置初始值就直接在构造函数里面设置掉