473,796 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(li nkLabel_Click);
}

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

if (this.Parent != null)
{
if (! this.Parent.Con trols.Contains( linkLabel))
{
this.Parent.Con trols.Add(linkL abel);
linkLabel.Locat ion = new Point(this.Righ t + 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.Heigh t = 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 10133
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*****@discus sions.microsoft .com> wrote in message
news:A9******** *************** ***********@mic rosoft.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(li nkLabel_Click);
}

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

if (this.Parent != null)
{
if (! this.Parent.Con trols.Contains( linkLabel))
{
this.Parent.Con trols.Add(linkL abel);
linkLabel.Locat ion = new Point(this.Righ t + 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.Heigh t = 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.Wid th, not
RadioButtonEx.W idth. 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(li nkLabel_Click);
}

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

if (this.Parent != null)
{
if (! this.Parent.Con trols.Contains( linkLabel))
{
this.Parent.Con trols.Add(linkL abel);
linkLabel.Locat ion = new Point(this.Righ t + 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.Heigh t = 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*****@discus sions.microsoft .com> wrote in message
news:0C******** *************** ***********@mic rosoft.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*******@cana da.com> wrote in message
news:11******** *************@m 73g2000cwd.goog legroups.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.Wid th, not
RadioButtonEx.W idth. 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*****@discus sions.microsoft .com> wrote in message
news:A9******** *************** ***********@mic rosoft.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(li nkLabel_Click);
}

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

if (this.Parent != null)
{
if (! this.Parent.Con trols.Contains( linkLabel))
{
this.Parent.Con trols.Add(linkL abel);
linkLabel.Locat ion = new Point(this.Righ t + 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.Heigh t = 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*****@discus sions.microsoft .com> wrote in message
news:A9******** *************** ***********@mic rosoft.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(li nkLabel_Click);
}

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

if (this.Parent != null)
{
if (! this.Parent.Con trols.Contains( linkLabel))
{
this.Parent.Con trols.Add(linkL abel);
linkLabel.Locat ion = new Point(this.Righ t + 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.Heigh t = 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

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

Similar topics

8
2267
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
6742
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) template<class X> void myfunc(X par1) { } (in derived class) template<class X>
10
5211
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
7012
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 class that inherits from it
4
1707
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
1933
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 the flexibility they introduce (flexibility really needed in framework developing), are often a nuisance for final developers. They don't like them because they never know if base class must be called and where should they place the call if...
2
2996
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 properties on TestManager into DocumentManager, and then load the TestManager properties with values from Document manager.... I am really confused all the examples I can find seem like simple Animal->Dog type instructions. Basically in...
2
3605
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 property without actually having to reimplement the get and set methods. In other words, override the the property without changing the functionality of the base property and without explicitly having to reimplement the get and set methods to make it do...
17
2921
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 when the Poodle is upcast to the Dog (in its wildest dreams) it then says "bow wow" where the bernard always says "woof" (see code). Basically, it appears that I'm hiding the poodle's speak method from everything except the poodle. Why would I...
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10012
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7548
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5442
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.