控件继承被表单设计者破坏。 [英] Control inheritance spoiled by Form designer.

查看:64
本文介绍了控件继承被表单设计者破坏。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用

子类控件时,表单设计器会向该部分添加不必要的代码。

我已经在VS.NET 2002和VS.NET 2003中重现了这一点所以它很漂亮

基本。


大纲步骤:

创建一个VB项目。

创建UI控件的子类,我使用TextBox。

不向子类添加代码。

Public Class Component1

继承系统.Windows.Forms.TextBox

#Region"组件设计器生成的代码

#End Region

结束类


仅更改前色属性(本例中为红色) 。

设计者将代码添加到InitialiseComponent()方法中以设置新的
前色。

< System.Diagnostics.DebuggerStepThrough() > Private Sub InitializeComponent()

''

''组件1

''

Me.ForeColor =系统。 Drawing.Color.Red

End Sub


构建它。


创建一个VB窗体项目。

拖放基类文本框。

添加对包含控件的DLL的引用。

将其添加到工具箱中。

拖放子类文本框。


一切看起来都不错。

但是查看代码时它不使用继承!!


表单设计器错误(IMO)添加了一行代码,它复制了子类控件'的InitialiseComponent()中的

代码,这完全否定了
继承 - 我在'InitialiseComponent()的形式中找到它。


''组件11

''

Me.Component11.ForeColor = System.Drawing.Color.Red


如果我将控制子类分配为单独的程序集(dll)然后

更新那个dll,编译后的表单有硬编码的代码来设置控件实例的

颜色。

那是不对的!

因为当我使用不同的

前驱分发新版本的控件时,控件的InitialiseComponent中的代码( )可能就像

那样不存在 - 因为VB表单有一行代码将颜色设置为当时使用的组件版本的颜色

表单是

编辑。

我可以从表格的'InitialiseComponent()中删除有问题的代码并且它

没有视觉效果。

颜色仍然是子类控件中的定义。

我可以更改控件的前部颜色,重建dll并且表单显示

新的前色。

表格中仍然没有违规代码,表格为InitialiseComponent()


现在我们进入暮光区...

在表单设计器中,如果我移动两个文本框中的任何一个,则会再次显示违规的

代码行。

那么,这是一个已知错误吗?
我不想把代码放在子类控件'的Paint()方法中,例如
,因为它经常被调用。


-


PAul

The form designer adds unnecessary code to the section when using a
subclassed control.
I''ve reproduced this in VS.NET 2002 and VS.NET 2003 so it''s pretty
fundamental.

Outline steps:
Create a VB project.
Create a subclass of a UI control, I used TextBox.
Add no code to the subclass.
Public Class Component1
Inherits System.Windows.Forms.TextBox
#Region " Component Designer generated code "
#End Region
End Class

Change the forecolour property only (Red in this example).
The designer adds code to the InitialiseComponent() method to set the new
forecolour.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
''
''Component1
''
Me.ForeColor = System.Drawing.Color.Red
End Sub

Build it.

Create a VB windows form project.
Drag/drop a baseclass textbox.
Add reference to the DLL containing the control.
Add that to the toolbox.
Drag/drop a subclassed textbox.

All appears good.
But looking at the code it''s not using inheritance !!

The form designer incorrectly (IMO) adds a line of code which duplicates the
code in the subclassed control''s InitialiseComponent() which totally negates
the inheritance - I find this in the form''s InitialiseComponent().

''Component11
''
Me.Component11.ForeColor = System.Drawing.Color.Red

If I distribute the control subclass as a separate assembly (dll) and then
update that dll, the compiled form has code that is hardwired to set the
colour of the instance of the control.
That''s not right!
Because when I distribute a new version of the control with a different
forecolour, the code in the contorl''s InitialiseComponent() might just as
well not be there - because the VB form has a line of code to set the colour
to that of the version of the component used at the time the form was
edited.
I can remove the offending code from the form''s InitialiseComponent() and it
has no visual effect.
The colour is still as defined in the subclassed control.
I can change the control''s forecolour, rebuild the dll and the form shows
the new forecolour.
There is still no offending code in the form InitialiseComponent()

Now we enter the twilight zone ...
In the form designer, if I move _either_ of the two textboxes, the offending
line of code reappears.
So, is this a known bug?
I don''t want to have to put code in the subclassed control''s Paint() method
for example, because that gets called far too often.

--

PAul

推荐答案

我现在转载这也是一个C#形式,使用相同的VB子类

文本框,所以它非常基础!


没有理由我可以看到表单应该有一个副本

来自子类控件'的InitialiseComponent()的代码,格式为'

InitialiseComponent() - 因为这将覆盖您在控件类中可能进行的任何更改。
I''ve now reproduced this in a C# form too, using the same VB subclass of
textbox, so it''s pretty fundamental!

There is no reason as far as I can see that the form should have a copy of
the code from the subclasses control''s InitialiseComponent() in the form''s
InitialiseComponent() - because that overrides any change you may make in
the control class.
表单设计器在使用
子类控件时会向该部分添加不必要的代码。
我代表在VS.NET 2002和VS.NET 2003中对此进行了描述,因此它非常基本。

大纲步骤:
创建一个VB项目。
创建一个UI控件的子类,我使用TextBox。
不向子类添加代码。
Public Class Component1
Inherits System.Windows.Forms.TextBox
#Region"组件设计器生成的代码
#End Region
End Class

仅更改forecolour属性(本例中为Red)。
设计器将代码添加到InitialiseComponent ()方法设置新的前色。
< System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()''
''Component1
''
Me.ForeColor = System.Drawing.Color.Red
End Sub
<构建它。

创建一个VB窗体项目。
拖放基类文本框。
添加对包含控件的DLL的引用。
添加那就是工具箱了。
拖放一个子类文本框。

一切看起来都不错。
但是查看代码时它没有使用继承!!

窗体设计器错误地(IMO)添加了一行代码,它复制
子类控件'的InitialiseComponent()中的代码,完全
否定继承 - 我在表单中找到它''的InitialiseComponent()。

''Component11
''
Me.Component11.ForeColor = System.Drawing.Color.Red

如果我将控制子类分发为一个单独的程序集(dll),然后
更新该dll,编译后的表单具有硬连线的代码来设置
颜色o f。控件的实例。
这是不对的!
因为当我使用不同的前色分发新版本的控件时,控件的InitialiseComponent中的代码()可能只是因为不存在 - 因为VB表单有一行代码将
颜色设置为表单时使用的组件版本的颜色
编辑。

我可以从表格'的InitialiseComponent()和
删除有问题的代码,它没有视觉效果。
颜色仍然是子类控件中的定义。
我可以更改控件的前部颜色,重建dll并且表单显示新的前部颜色。
还没有违反代码的形式InitialiseComponent()

现在我们进入了暮光之城......
在表单设计器中,如果我移动两个文本框中的一个,则会再次出现
违规行代码。

那么,这是一个已知的错误吗?
我不想放c例如,在子类控件的'Paint()
方法中的ode,因为它经常被调用。

-

PAul
The form designer adds unnecessary code to the section when using a
subclassed control.
I''ve reproduced this in VS.NET 2002 and VS.NET 2003 so it''s pretty
fundamental.

Outline steps:
Create a VB project.
Create a subclass of a UI control, I used TextBox.
Add no code to the subclass.
Public Class Component1
Inherits System.Windows.Forms.TextBox
#Region " Component Designer generated code "
#End Region
End Class

Change the forecolour property only (Red in this example).
The designer adds code to the InitialiseComponent() method to set the new
forecolour.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() ''
''Component1
''
Me.ForeColor = System.Drawing.Color.Red
End Sub

Build it.

Create a VB windows form project.
Drag/drop a baseclass textbox.
Add reference to the DLL containing the control.
Add that to the toolbox.
Drag/drop a subclassed textbox.

All appears good.
But looking at the code it''s not using inheritance !!

The form designer incorrectly (IMO) adds a line of code which duplicates the code in the subclassed control''s InitialiseComponent() which totally negates the inheritance - I find this in the form''s InitialiseComponent().

''Component11
''
Me.Component11.ForeColor = System.Drawing.Color.Red

If I distribute the control subclass as a separate assembly (dll) and then
update that dll, the compiled form has code that is hardwired to set the
colour of the instance of the control.
That''s not right!
Because when I distribute a new version of the control with a different
forecolour, the code in the contorl''s InitialiseComponent() might just as
well not be there - because the VB form has a line of code to set the colour to that of the version of the component used at the time the form was
edited.
I can remove the offending code from the form''s InitialiseComponent() and it has no visual effect.
The colour is still as defined in the subclassed control.
I can change the control''s forecolour, rebuild the dll and the form shows
the new forecolour.
There is still no offending code in the form InitialiseComponent()

Now we enter the twilight zone ...
In the form designer, if I move _either_ of the two textboxes, the offending line of code reappears.
So, is this a known bug?
I don''t want to have to put code in the subclassed control''s Paint() method for example, because that gets called far too often.

--

PAul



" PAul Maskens" <下午****** @ mvps.org> schrieb
"PAul Maskens" <pm******@mvps.org> schrieb
使用
子类控件时,表单设计器会向该部分添加不必要的代码。
我已经在VS.NET 2002和VS.NET 2003中重现了这一点,所以它很漂亮
基本。
[...]
The form designer adds unnecessary code to the section when using
a subclassed control.
I''ve reproduced this in VS.NET 2002 and VS.NET 2003 so it''s pretty
fundamental.
[...]




这不是继承问题。设计者对

继承层次结构不感兴趣。它创建代码的原因是

属性值(红色)不等于属性的默认值。


参见(didn'阅读整篇文章):
http://msdn.microsoft.com/library/en...ustcodegen.asp


引用:" ...如果当前值为a属性匹配默认值,

代码未生成。


-

Armin


如何报价以及原因:
http: //www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html



It is not an inheritance issue. The designer is not interested in the
inheritance hierarchy. The reason why it creates the code is that the
property value (red) is not equal to default value of the property.

See also (didn''t read the whole article):
http://msdn.microsoft.com/library/en...ustcodegen.asp

quote: "...If the current value of a property matches that default value,
code is not generated."

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html


>它不是继承问题。

因为它阻止我们使用继承来定义视觉特性,

并在包含子类的程序集的新版本中重新定义,它是

继承问题。
> It is not an inheritance issue.
Becauase it prevents us using inheritance to define visual characteristics,
and redefine in a new release of the assembly containing the subclass, it is
an inheritance issue.
设计者对继承层次结构不感兴趣。
确切地说,设计师正在破坏继承。

创建代码的原因是属性值(红色)
不等于属性的默认值。
他们设计时吸烟了什么呢?

引用:" ...如果属性的当前值与该默认值匹配,则不会生成代码。
The designer is not interested in the inheritance hierarchy. Exactly, so the designer is breaking inheritcance.
The reason why it creates the code is that the property value (red)
is not equal to default value of the property. What were they smoking when they designed that, then?
quote: "...If the current value of a property matches that default value,
code is not generated."



那么问题是,如何在我的子类中定义新值为

新默认值?


它不是一个表演限制器,但我已经使用OO语言十年了

这是令人难以置信的。


So the question then is, how do I define the new value in my subclass as the
new default?

It''s not a show stopper, but I''ve been using OO languages for ten years and
this is unbelievable.


这篇关于控件继承被表单设计者破坏。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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