2017-06-13 17 views
0

を構築した後、自身をリセットtextChangedしかし、私が手動で値をプロパティウィンドウからfalseに手動で変更した場合でも、私はプロジェクトを構築するたびに、プロパティの値自体がtrueにリセットされます。C#のDefaultValueは、私は私が作るしようとしているカスタムテキストボックスに問題を抱えている

[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); 
    } 

答えて

0

バッキングフィールドを持つ試してみて、それを使用する:あなたはtrueに設定する必要がある[のDefaultValue(typeof演算(ブール値)、 "真")]で

private bool _AutoSize = true; 

    [Browsable(true)] 
    new public bool AutoSize 
    { 
     get { return _AutoSize; } 
     set 
     { 
      _AutoSize = value; 
      UpdateStyles(); 
     } 
    } 
+0

すでに試しても動作しません。こちらを参照してください。https://support.microsoft.com/it-it/it/it/it/it/it/help/311339/msdn-documentation-for-the-defaultvalueattribute-class-may-be-confusing – Signum

+0

@ Signumは私の編集された答えを参照 – CodingYoshi

+0

その1つ、結果が無かった – Signum

0
public class SampleEx : TextBox 
    { 
     [Browsable(true)] 
     [DefaultValue(typeof(bool), "true")] 
     new public bool AutoSize { get; set; } 

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

とコンストラクタ。 それはあなたのために働くでしょう。

+0

これも機能します、ありがとう – Signum

関連する問題