Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#默认值在生成后自行重置_C#_Textbox_Components - Fatal编程技术网

C#默认值在生成后自行重置

C#默认值在生成后自行重置,c#,textbox,components,C#,Textbox,Components,我试图创建的自定义文本框中存在问题: 我正在尝试创建一个默认值为true的新属性,以允许TextBox在textChanged后自行调整大小,但每次我构建项目时,即使我手动将属性窗口中的值更改为false,属性的值也会自动重置为true [Browsable(true)] new public bool AutoSize { get; set; } = true; protected override void OnTextChanged(EventArgs e)

我试图创建的自定义文本框中存在问题:

我正在尝试创建一个默认值为true的新属性,以允许TextBox在textChanged后自行调整大小,但每次我构建项目时,即使我手动将属性窗口中的值更改为false,属性的值也会自动重置为true

    [Browsable(true)]
    new public bool AutoSize { get; set; } = true;  

    protected override void OnTextChanged(EventArgs e)
    {
        if (AutoSize == true)
        {
            Size size = TextRenderer.MeasureText(Text, Font);
            Width = size.Width;
            Height = size.Height;
        }
        base.OnTextChanged(e);
    }

尝试使用支持字段,并使用该字段:

    private bool _AutoSize = true;

    [Browsable(true)]
    new public bool AutoSize
    {
        get { return _AutoSize; }
        set
        {
            _AutoSize = value;
            UpdateStyles();
        }
    }
在[DefaultValue(typeof(bool),“true”)]和构造函数中,必须设置为true。
它对您有效。

已经尝试过,但不起作用,请阅读此处您的方法可以添加UpdateStyles();在集合的末尾(在集合内部,在this.autosize=value;)之后)。修改它,以便我可以为其他人将答案设置为“已解决”
public class SampleEx : TextBox
    {
        [Browsable(true)]
        [DefaultValue(typeof(bool), "true")]
        new public bool AutoSize { get; set; }

        public SampleEx()
            : base()
        {
            this.AutoSize = true;            
        }
    }