覆盖基本属性 [英] Overriding Base Properties

查看:61
本文介绍了覆盖基本属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题涉及到我正在尝试用CF做的事情,但它真的是

a泛型C#问题。


随着契约框架,人们不能添加带有长标签的单选按钮

,因为标签不会换行。所以我决定创建自己的定制收音机

按钮。随着我的版本将有一个没有文字的基本单选按钮和

它的大小减少到12 x 12.然后在这旁边我将放置一个LinkLabel,

将包装,给出一个多行标签的外观。


这是我到目前为止写的基本代码:


公共类RadioButtonEx:RadioButton

{

LinkLabel linkLabel = new LinkLabel();


public RadioButtonEx()

{

base.Width = 12;

base.Height = 12;

linkLabel.Click + = new EventHandler( linkLabel_Click);

}


私有字符串文本;

公共覆盖字符串文本

{

get

{

返回文字;

}

set

{

text = value;

linkLabel.Text = value;


if(this.Parent!= null)

{

if(!this.Parent.Controls.Contains(linkLabel))

{

this.Pare nt.Controls.Add(linkLabel);

linkLabel.Location = new Point(this.Right + 6,this.Top);

}

}

}

}


私有字符串宽度;

public override int Width

{

get

{

返回宽度;

}

套装

{

linkLabel.Width = value - 18;

}

}


私人字符串高度;

公共覆盖int高度

{

get

{

返回高度;

}

设定

{

linkLabel .Height = value;

}

}


private void linkLabel_Click(object sender,EventArgs e)

{

this.Checked = true; //如果点击标签然后检查收音机

按钮

}

}

但是我''关于获取和设置的宽度&

得到这种错误高度:


无法覆盖继承的成员''System.Windows.Forms。 Control.Width.set''

因为它没有标记为虚拟,抽象或覆盖

如何解决这个问题?


-

Robert W.

温哥华,BC
www.mwtech.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

推荐答案

使用new修饰符隐藏它:


new public int宽度

{

get

{

返回宽度;

}

设定

{

linkLabel.Width = value - 18;

}

}


注意:当您以这种方式隐藏继承的成员时,您将失去对
$ b $的所有访问权限它来自外面 班级。从*里面*上课,你仍然可以使用base.Width来获得它。




-

HTH ,


Kevin Spencer

微软MVP

专业鸡肉沙拉炼金术士


Big厚厚的是由许多小薄片组成的。

Robert W. <滚装***** @ discussions.microsoft.com>在消息中写道

新闻:A9 ********************************** @ microsof t.com ...
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...
这个问题涉及我正在尝试用CF做的事情,但它确实是一个普通的C#问题。

使用Compact Framework,无法添加带有长标签的单选按钮,因为标签不会换行。所以我决定创建自己的定制
收音机
按钮。在我的版本中会有一个基本的单选按钮,没有文字
和它的大小减少到12 x 12.然后在这旁边我将放置一个LinkLabel,
将包装,给予多线标签的外观。

这是我到目前为止写的基本代码:

公共类RadioButtonEx:RadioButton
{
LinkLabel linkLabel = new LinkLabel();
公共RadioButtonEx()
{
base.Width = 12;
base.Height = 12;
linkLabel.Click + = new EventHandler(linkLabel_Click);
}
私有字符串文本;
公共覆盖字符串文本
{
获取
{
text = value;
linkLabel.Text = value;
if(this.Parent!= null)
{
if(!this.Parent.Controls.Contains(linkLabel))
{
this.Parent.Controls。添加(linkLabel);
linkLabel.Location = new Point(this.Right + 6,this.Top);
}
}
}
}
私人字符串宽度;
公共覆盖int宽度
{
获得
{
返回宽度;
}
设置
{
linkLabel.Width = value - 18;
}

私人字符串高度;
公共覆盖int高度
{
获得
{
返回高度;
}
设置
{linkLabel.Height = value;
}
}

private void linkLabel_Click(object sender,EventArgs e)
{
this.Checked = true; //如果点击标签然后检查收音机
按钮
}

但是我得到关于获取和设置的这类错误宽度&
高度:

不能覆盖继承的成员''System.Windows.Forms.Control.Width.set''
因为它没有标记为虚拟,抽象,或覆盖

如何解决这个问题?

- 罗伯特W.
温哥华,不列颠哥伦比亚省
www.mwtech.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



Kevin,


但我实际上想要访问Width&来自课外的高度。

设想一个没有文字的单选按钮和旁边的LinkLabel来代表其文本的
。如果我将单选按钮的宽度设置为200,那么我想要

设置修饰符Width中的代码以保持实际

单选按钮的宽度在12,而是将LinkLabel的宽度设置为200 - 12 - 6

= 182(6提供了一点间隙)


进一步做一些阅读我在想引入新的物业

FullWidth和FullHeight是要走的路。你觉得怎么样?


-

Robert W.

温哥华,不列颠哥伦比亚省
www.mwtech.com


" Kevin Spencer"写道:
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:
你使用new修饰符隐藏它:

new public int Width
{
get
{
返回宽度;
}
设置
{
linkLabel.Width = value - 18;
}
}

-
HTH,

Kevin Spencer <微软MVP
专业鸡肉沙拉炼金术士

大厚片由很多小薄片组成。
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.








Kevin Spencer写道:

Kevin Spencer wrote:
注意:当你以这种方式隐藏继承的成员时,你将失去对
它的所有访问权限。类。从*里面*班级,你仍然可以通过使用base.Width来获得它。
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.




这是正确的,但具有误导性。


每当你覆盖任何基类属性,方法,事件等时

使用覆盖和覆盖 _or_" new",你失去了从课堂外访问

对应的_base class_成员的能力。


也就是说,你是否说覆盖宽度或者新宽度,你的
无法从你的新课程外的

基类RadioButton中获得原始宽度。每当你说:


RadioButtonEx rbe = new RadioButtonEx();

rbe.Width


你得到了宽度在RadioButtonEx中声明,而不是在RadioButton中声明的宽度

,这是你想要的。


new之间的一个区别然而,覆盖是指你将RadioButtonEx投射到RadioButton,因为你想将它与

常规单选按钮混合并对它们进行相同处理:


RadioButton rb = new RadioButtonEx();

rb.Width


不幸的是,你会调用RadioButton 。宽度,不是

RadioButtonEx.Width。这就是使用new的问题。


然而,在你的情况下,唯一的另一个选择是引入新的

属性,就像你一样提及。最后,这可能是你做的最安全的事情。



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.


这篇关于覆盖基本属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆