Removing properties from usercontrol

  • Thread starter Thread starter John Baro
  • Start date Start date
J

John Baro

I have a usercontrol that inherits from control.
I want to "remove" properties that are not relevant (backcolor,
backgroundimage) etc..
How can this be accomplished?
TIA
John
 
Hi John

In my understanding of things, you can't.

What you have to do is dump the inheritence and create an instance of the
object you're inheriting from inside your object and then rewrite the
relevant properties to access the properties of the control you wish to be
visible whilst masking the rest. I asked a similar question not two days
ago. See below for the question and it's answer.

Regards

Greg
I have a C# class libary (Class1) which is inherited by another class in a
Windows Forms app. I want this other class to implement some, but not all of
the features of the base class. How can I completely hide (within the IDE)
the members of the base class library i.e. only expose those members that I
want exposed?

You can't. If you don't want people to have access to the base class's
members, don't inherit from it. Have a search for Liskov's
Substitutability Principle for good reasons for this.
I've tried some methods gleaned by googling for "hiding base
class members" which suggested using things like "new" and a blank function
structure but this doesn't apparently work. The only idea I've come up with
is not to inherit the base class but simply create an instance of it and use
that instead and then create only the methods and properties that I need in
my derived class. This at the moment seems to be the most logical
solution,

Yup, it is.
but it also seems like a lot of extra work, does anyone know of a better
way. I basically want it to work like using interfaces, you know, expose and
hide what you want as appropriate.

Composition can be a bit of extra work, but gives a lot of flexibility
and can avoid unintended uses of your class.
 
Thanks Greg

C-Sharper or C-Hasher said:
Hi John

In my understanding of things, you can't.

What you have to do is dump the inheritence and create an instance of the
object you're inheriting from inside your object and then rewrite the
relevant properties to access the properties of the control you wish to be
visible whilst masking the rest. I asked a similar question not two days
ago. See below for the question and it's answer.

Regards

Greg
all that

You can't. If you don't want people to have access to the base class's
members, don't inherit from it. Have a search for Liskov's
Substitutability Principle for good reasons for this.

solution,

Yup, it is.


Composition can be a bit of extra work, but gives a lot of flexibility
and can avoid unintended uses of your class.
 
You cannot remove members of the base class, but you can hide them from the
visual designer so that they are not shown in the properties window.

simply override the members you don't want to see and apply the
BrowsableAttribute to it:

[Browsable(false)]
public override Color BackColor
{
get{..}
set{..}
}
 
Back
Top