Visual Inheritance - How to make public inherited properties private or readonly?

  • Thread starter Thread starter Michael.Suarez
  • Start date Start date
M

Michael.Suarez

Let's say I create a custom component that inherits from TextBox. Then
I set the color of the textbox. Now I want it so that anyone who uses
this component can not change the color. How do i make it so that the
BackColor property is no longer able to be set (either at runtime or
design time) for my new custom text box?
 
Let's say I create a custom component that inherits
from TextBox. Then I set the color of the textbox.
Now I want it so that anyone who uses this component
can not change the color.

You can't "hide" a base class' public member, as it would defeat the
point of polymorphism. Perhaps you can override OnBackColorChanged and
immediately set the colour back to what it was before.

Eq.
 
Paul is right: you can't _completely_ hide the BackColor property, but
perhaps this might work for you:

private new Color BackColor
{
get { return base.BackColor; }
set { base.BackColor = value; }
}

If you just want it hidden in the property browser at design time, but
not hidden in code, you can do this:

[Browsable(false)]
public override Color BackColor
{
get { return base.BackColor; }
set { base.BackColor = value; }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top