0

I want to create a new class inherited from button (Winform). I want to "remove" the Text property and replace it by my own property :

public partial class ButtonLetter : Button
{
    public char Letter
    {
        get
        {
            char _letter;
            if (char.TryParse(Text, out _letter))
                return _letter;
            else
                return ' ';
        }
        set
        {
            Text = value.ToString();
        }
    }
    private new string Text
    {
        get
        {
            return base.Text;
        }

        set
        {
            base.Text = value;
        }
    }

}

But when I use this control, the designer creates this code :

this.buttonLetter1.Letter = 'A';
this.buttonLetter1.Name = "buttonLetter1";
this.buttonLetter1.Text = "buttonLetter1";

And it works, the Text property is modify and my button contains "buttonLetter1" instead of "A".

Is there a way to mask the Text property to prevent its access ? (In this case the property should not be visible in designer => it is considered as private)

A.Pissicat
  • 3,023
  • 4
  • 38
  • 93
  • @Sinatr I've try this way but this solution don't prevent to use `Text` property. I want that my `ButtonLetter` acts like property `Text` is private – A.Pissicat Apr 04 '17 at 09:29
  • There is no way of preventing access, even with the `new` keyword. In the mentioned example, simply casting the object to a `Button` allows someone to access the original `Text` property: `((Button)this.ButtonLetter1).Text = "Foobar";` – ikkentim Apr 04 '17 at 09:34
  • Why do you want to make it `private`? Can you explain what you want to archive and why `public` text is a problem? – Sinatr Apr 04 '17 at 09:54
  • @Sinatr Cause my button must only display a char. I want to force user to use my property `Letter` typed as char instead of `Text` typed as string – A.Pissicat Apr 04 '17 at 10:58

0 Answers0