Overriding Base Properties

G

Guest

This question concerns something I'm trying to do with the CF but it's really
a generic C# question.

With the Compact Framework, one can't add a radio button with a long label
because the labels don't wrap. So I've decided to create my own custom radio
button. With my version there will be a basic radio button with no text and
its size reduced to 12 x 12. Then beside this I will place a LinkLabel,
which will wrap, to give the appearance of a multi-line label.

Here's the basic code I've written so far:

public class RadioButtonEx : RadioButton
{
LinkLabel linkLabel = new LinkLabel();

public RadioButtonEx()
{
base.Width = 12;
base.Height = 12;
linkLabel.Click += new EventHandler(linkLabel_Click);
}

private string text;
public override string Text
{
get
{
return text;
}
set
{
text = value;
linkLabel.Text = value;

if (this.Parent != null)
{
if (! this.Parent.Controls.Contains(linkLabel))
{
this.Parent.Controls.Add(linkLabel);
linkLabel.Location = new Point(this.Right + 6, this.Top);
}
}
}
}

private string width;
public override int Width
{
get
{
return width;
}
set
{
linkLabel.Width = value - 18;
}
}

private string height;
public override int Height
{
get
{
return height;
}
set
{
linkLabel.Height = value;
}
}

private void linkLabel_Click(object sender, EventArgs e)
{
this.Checked = true; // If the label is clicked then check the radio
button
}
}


But I'm getting this sort of error about the gets and sets of the Width &
Height:

Cannot override inherited member 'System.Windows.Forms.Control.Width.set'
because it is not marked virtual, abstract, or override


How do I resolve this?
 
K

Kevin Spencer

You hide it by using the new modifier:

new public int Width
{
get
{
return width;
}
set
{
linkLabel.Width = value - 18;
}
}

Note: When you hide the inherited member in this way, you lose all access to
it from outside the class. From *inside* the class, you can still get to it
by using base.Width.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Big thicks are made up of lots of little thins.
 
G

Guest

Kevin,

But I actually want to access Width & Height from outside the class.
Envision a radio button with no text and a LinkLabel beside it to represent
its text. If I set the width of the radio button to say, 200, then I want
the code inside the set modifier of Width to keep the width of the actual
radio button at 12 and instead set the width of the LinkLabel to 200 - 12 - 6
= 182 (the 6 provides a little gap)

Upon doing some further reading I'm thinking that introducing new properties
FullWidth and FullHeight are the way to go. What do you think?
 
B

Bruce Wood

Kevin said:
Note: When you hide the inherited member in this way, you lose all access to
it from outside the class. From *inside* the class, you can still get to it
by using base.Width.

This is correct but misleading.

Whenever you override any base class property, method, event, etc.
using either "override" _or_ "new", you lose the ability to access the
corresponding _base class_ member from outside the class.

That is to say, whether you say "override Width" or "new Width", you
cannot get at the original width in the base class RadioButton from
outside your new class. Whenever you say:

RadioButtonEx rbe = new RadioButtonEx();
rbe.Width

you are getting the "Width" declared in RadioButtonEx, not the Width
declared in RadioButton, which is what you want.

One difference between "new" and "override", however, is if you cast
your RadioButtonEx to a RadioButton because you want to mix it with
regular radio buttons and treat them all the same:

RadioButton rb = new RadioButtonEx();
rb.Width

you will, unfortunately, invoke RadioButton.Width, not
RadioButtonEx.Width. That's the problem with using "new".

However, in your situation the only other choice is to introduce new
properties, as you mentioned. In the end, that's probably the safest
thing for you to do.
 
G

Guest

Hi Robert,
I saw the posts relating to using the new keyword, like Bruce mentioned
it does have the unfortunate side effect in your case of making the
implementation of the method rely on the type of the reference variable.

Another thing you can do since you are only trying to change the behaviour
of the Width and height properties is to capture the WidthChanged event when
this occurs you can simply remove your event handler for that event, change
the width then readd the event handler (to make sure you do not get into
infinite recursion). This is not the most elegant of solutions but it may
work for you.

Mark
http://www.markdawson.org
 
K

Kevin Spencer

Anything accessible from inside a class can be accessed from outside the
class via a property. Example:

You hide it by using the new modifier:

new public int Width
{
get
{
return width;
}
set
{
linkLabel.Width = value - 18;
}
}

public int BaseWidth { get { return base.Width; } set { base.Width =
value } }

So, you can either hide the inherited member, or you can use a different
member name for the new member. It all depends on your requirements.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Big thicks are made up of lots of little thins.
 
K

Kevin Spencer

Whenever you override any base class property, method, event, etc.
using either "override" _or_ "new", you lose the ability to access the
corresponding _base class_ member from outside the class.

That is exactly what I said:

You called it the "corresponding_base class_member". I called it "the
inherited member." Which of us is being "misleading" would be, I suppose, a
subjective interpretation, depending upon which term ("base" or "inherited")
the person reading has the most trouble understanding.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Big thicks are made up of lots of little thins.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Two things, first there is a NG devoted to the CF you could find there
useful info regarding the CF.

Also take a look at www.opennetcf.org they have a very extensive library of
controls to extend the provided by the CF.
 
G

Guest

Ignacio,

Yes, I frequently post in the CF newsgroup but thought this question was
more a generic one about the C# language.

I have OpenNETCF 1.4 installed but unfortunately it doesn't have a
replacement radio button. I seem to be succeeding by using a radio button
with no text and a LinkLabel "connected" to this radio button. Just a little
bit more tweaking and I should have it working!
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,
I have OpenNETCF 1.4 installed but unfortunately it doesn't have a
replacement radio button. I seem to be succeeding by using a radio button
with no text and a LinkLabel "connected" to this radio button. Just a
little
bit more tweaking and I should have it working!

May I suggest that you let them know about your new control? maybe they are
interesting in including it in the next version of the framework
 
G

Guest

Right now it's tied in with my own underlying framework. But in the future,
if I can make it more generic, then I will.

I am surprised that MS hasn't come up with a radio button with wrappable
text though. Maybe in a future version!
 
B

Bruce Wood

Kevin said:
That is exactly what I said:

Sorry. I should have been more explicit about what I considered
"inaccurate". I was focusing on the words "in this way": when you hide
the inherited member "in this way" (i.e. using "new") then you lose all
access to it from outside the class.

In fact, if you hide the inherited member in either way: either using
"new" or "override" then you lose access to the base class member from
outside the class. The only exception is if you use "new" to hide the
inherited member, when you can get at the base class member by casting
to a base class reference.

So, in the end I just wanted to clarify your post: what you said was
also true for "override," and there was one exception to what you
stated.
 
K

Kevin Spencer

Hi Bruce,

I had not thought of the interpretation of what I said the way you thought
of it. At any rate, by now I'm sure that all possible misunderstanding has
been erased!

--
;-),

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Big thicks are made up of lots of little thins.
 

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