How to hide an inherited property.

A

active

If I create a UserControl that inherits from, say, Panel, and I do not want
my UserControl to have a property that it inherits from Panel. How can I
remove it.

For example, I might not want by control to have a BackColor property even
though it inherited that property from Panel.



Thanks
 
M

Mattias Sjögren

If I create a UserControl that inherits from, say, Panel, and I do not want
my UserControl to have a property that it inherits from Panel. How can I
remove it.

Then inheritance probably isn't the right approach to take. Maybe your
control should contain a Panel instead?


Mattias
 
A

active

That would not work, I need inheritance, but I want to remove one property
because I set it in my control.

thanks
 
A

AlexS

Even if example maybe isn't ideal one,

private new Color BackColor {...}

doesn't hide base.BackColor. BackColor is accessible and compiles OK in
inheriting classes and external classes. Which seems to contradict
description of private and new modifiers in MSDN.

To me this looks wrong. C# 2.0.
 
J

Joergen Bech

There was a discussion about it five years ago with a very good
reply: http://tinyurl.com/2hs9f3

Basically, you can hide it from the propertygrid and from
serialization, etc, but not from someone who wants to set
it manually through code, though you could simply override
the property and ignore whatever value is set or throw an
exception if an attempt is made to set it from outside the
class.

But if your control has some kind of BackColor behavior,
why not just override the property and react sensibly and
predictably to whatever value the user sets? That would
be prettier from a component design perspective.

Regards,

Joergen Bech



That would not work, I need inheritance, but I want to remove one property
because I set it in my control.

thanks
 
A

active

I tried this and it appears to work.

I created a Shadowed Read only Property

I haven't tested it extensively but it appears to work OK.

What do you think about that approach?


Joergen Bech @ post1.tele.dk> said:
There was a discussion about it five years ago with a very good
reply: http://tinyurl.com/2hs9f3

Basically, you can hide it from the propertygrid and from
serialization, etc, but not from someone who wants to set
it manually through code, though you could simply override
the property and ignore whatever value is set or throw an
exception if an attempt is made to set it from outside the
class.

But if your control has some kind of BackColor behavior,
why not just override the property and react sensibly and
predictably to whatever value the user sets? That would
be prettier from a component design perspective.

Regards,

Joergen Bech
 
A

active

I tried this and it appears to work.

I created a Shadowed Read only Property

I haven't tested it extensively but it appears to work OK.

What do you think about that approach?
 
J

Joergen Bech

Yes, something like this:

---snip---
Public Class MyPanel
Inherits Panel

<Browsable(False),
EditorBrowsable(EditorBrowsableState.Never)> _
Public Shadows ReadOnly Property BackColor() As
System.Drawing.Color
Get
Return MyBase.BackColor
End Get
End Property

End Class
---snip---

I was wrong in my first test/reply: It IS possible to
hide it from the editor using one of the attributes in
the example above. Sorry about that.

One side-effect of this is that it is hidden from the
editor within the class itself - not just inherited classes,
so you have to use MyBase.BackColor instead of
Me.BackColor to get at it.

Regards,

Joergen Bech
 
A

active

That's what I did except for the attributes. I'm not sure if they buy
anything.

Thanks a lot
 

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

Top