开发者

how to make pre-set public property to private while creating a custom control

I am creating a custom MaskedTextBox for Date only. Now i want开发者_如何学Go to hide Mask property of MaskedTextBox from its users.

EDIT:

public class CustomDateMask:System.Windows.Forms.MaskedTextBox
    {

        public new string Mask
        {
           get;
           private set;
        }

        public  CustomDateMask()
        {
            this.Mask = @"00/00/2\000"; // this property should not be set by any one now
            this.ValidatingType = typeof(System.DateTime); // this property should not be set by any one now
        }
   }

What should i do so that no one can set this property


You can't completely remove it without breaking the Liskov substitution principal; but you could re-declare it and hide it (either make it non-browsable or make the setter private).

But IMO it is a bad idea; wrapping the control would be cleaner.

Note that member-hiding can be easily avoided (without even realising) simply by casting as the base-type.


 public class Test : System.Windows.Forms.TextBox
 {
    public new string Text
    {
        get { return base.Text; }
        private set { base.Text = value; }
    }

    public Test()
    {
      base.Text = "hello";
    }
 }


 Test test = new Test(); // Create an instance of it
 string text = test.Text;
 text.Text = "hello world"; // comp error 

Error Details:

Error 1 The property or indexer 'ScratchPad.Test.Text' cannot be used in this context because the set accessor is inaccessible C:\@Dev\ScratchPad\ScratchPad\ScratchPad\Form1.cs 33 13 ScratchPad


Copy and paste this into your class:

[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new string Mask {
    get { return base.Mask; }
    set { base.Mask = value; }
}

The [Browsable] attribute hides the property in the Properties window. The [EditorBrowsable] attribute hides it from IntelliSense.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜