views:

76

answers:

3

I am creating a custom MaskedTextBox for Date only. Now i want 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

+3  A: 
 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

gmcalab
This is a: easily avoided by casting to the base type, and b: doesn't return the (base) mask at all.
Marc Gravell
@gmcalab: Even after doing this, I am able to set Mask Property. It hasn't hidden yet
Shantanu Gupta
@Shantanu Gupta, I dont know what you did but I update mine to show the whole code. It errors for me if i try to set it.
gmcalab
@gmcalab: you are right, it shows error. I was thinking that it should get hidden from property window which was answered by "nobugz" but your answer is also correct. Thx
Shantanu Gupta
+4  A: 

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.

Marc Gravell
Wrapping the control is the "right" way to do this, since this date box is *not* a `MaskedTextBox` . And you avoid the cast-as-base-type problem.
Travis Gockel
+1  A: 

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.

Hans Passant
@nobugz: do we need to use any namespace also for browsable
Shantanu Gupta
using System.ComponentModel; Visual Studio 2008 should prompt you add it automatically though....
Greg