473,378 Members | 1,411 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Overriding Base Properties

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?

--
Robert W.
Vancouver, BC
www.mwtech.com

Jun 26 '06 #1
12 10103
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.
"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:A9**********************************@microsof t.com...
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?

--
Robert W.
Vancouver, BC
www.mwtech.com

Jun 26 '06 #2
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?

--
Robert W.
Vancouver, BC
www.mwtech.com

"Kevin Spencer" wrote:
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.


Jun 27 '06 #3

Kevin Spencer wrote:
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.

Jun 27 '06 #4
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
"Robert W." wrote:
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?

--
Robert W.
Vancouver, BC
www.mwtech.com

Jun 27 '06 #5
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.
"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:0C**********************************@microsof t.com...
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?

--
Robert W.
Vancouver, BC
www.mwtech.com

"Kevin Spencer" wrote:
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.

Jun 27 '06 #6
> 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:
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.


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.
"Bruce Wood" <br*******@canada.com> wrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
Kevin Spencer wrote:
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.

Jun 27 '06 #7
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.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:A9**********************************@microsof t.com...
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?

--
Robert W.
Vancouver, BC
www.mwtech.com

Jun 27 '06 #8
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!

--
Robert W.
Vancouver, BC
www.mwtech.com

"Ignacio Machin ( .NET/ C# MVP )" wrote:
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.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:A9**********************************@microsof t.com...
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?

--
Robert W.
Vancouver, BC
www.mwtech.com


Jun 27 '06 #9
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
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jun 27 '06 #10
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!

--
Robert W.
Vancouver, BC
www.mwtech.com

"Ignacio Machin ( .NET/ C# MVP )" wrote:
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
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Jun 27 '06 #11

Kevin Spencer wrote:
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:
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.


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.

Jun 27 '06 #12
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.
"Bruce Wood" <br*******@canada.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...

Kevin Spencer wrote:
> 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:
>> 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.


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.

Jun 27 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Edward Diener | last post by:
Is it possible for a derived class to override a property and/or event of its base class ?
8
by: Massimiliano Alberti | last post by:
Can I specialize a template function in a subclass without overriding it? (the main template function is defined in a base class). Now I'm doing something like that: (in base class)...
10
by: muscha | last post by:
I don't get it. What's the main differences between overriding a base class's methods vs. hiding them? Thanks, /m
4
by: Mark Sizer | last post by:
Hi people, I'm having trouble trying to achieve something in C# and win forms, and am looking for a little advice if anyone has a moment. I've got two classes: 1) a base class 2) a derived...
4
by: Nilesh | last post by:
I am confused about the purpose of 'new' in overriding. Consider following example. <code_snippet> using console = System.Console; public class TestClass { public static void Main() {
4
by: ORi | last post by:
Hi all ! There's a question I've been bothering for a while: I'm actually developing architectural frameworks for application developing and I think virtual methods, although needed because of...
2
by: MattB | last post by:
Hello I have base class called DocumentManager.cs I then have another class which inherits from DocumentManager which is a TestManager.cs I want to override the base method and want to pass...
2
by: ESPNSTI | last post by:
Hi, I'm very new to C# and .Net, I've been working with it for about a month. My experience has been mainly with Delphi 5 (not .Net). What I'm looking for is for a shortcut way to override a...
17
by: Bob Weiner | last post by:
What is the purpose of hiding intead of overriding a method? I have googled the question but haven't found anything that makes any sense of it. In the code below, the only difference is that...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.