形式'生命周期 - 继承 [英] Forms' LifeCycle - Inheritance

查看:63
本文介绍了形式'生命周期 - 继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我有一个BaseForm,其中包含(例如)组件中的一个计时器& Controls''Collection中的一个按钮

还有三个Subs:Load,ScanControls& ScanComponents。

BaseForm仅用于继承(并继承自System.Windows.Forms.Form)。

Private Sub BaseForm_Load(ByVal sender As Object,ByVal e As System.EventArgs)处理MyBase.Load


ScanControls()


ScanComponents()


End Sub


私人子扫描控制()


Dim [Control] As System.Windows.Forms.Control


For Each [Control] In Me.Controls


''通过ControlCollection递归扫描


''Put在HashTableControls中


''做一些东西


下一页


结束Sub


Private Sub ScanComponents()


Dim [Component] As System.ComponentModel.Component


For Each [组件]在Me.components.Components


''放入HashTableComponents


''做一些东西


下一页


End Sub

我有另一个表格(例如:DisplayForm)继承自BaseForm。

它添加(例如)一个标签&一个ImageList。


a ..如果我查看ControlsCollection(HashTableControls),我会查找一个按钮&一个标签(这是正确的)

b ..如果我查看ComponentsCollection(HashTableComponents)我只会找到一个计时器(这是错误的:它还应该包含ImageList)

这种方法有什么问题?


TIA,

Michael

Hello,

I have a BaseForm with (eg) one Timer in the Components''collection & one Button in the Controls''Collection
Also there are three Subs: Load, ScanControls & ScanComponents.
The BaseForm is only used to be inherited from (and inherits from System.Windows.Forms.Form).
Private Sub BaseForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

ScanControls()

ScanComponents()

End Sub

Private Sub ScanControls()

Dim [Control] As System.Windows.Forms.Control

For Each [Control] In Me.Controls

'' Scan recursive through the ControlCollection

'' Put in HashTableControls

'' Do some stuff

Next

End Sub

Private Sub ScanComponents()

Dim [Component] As System.ComponentModel.Component

For Each [Component] In Me.components.Components

'' Put in HashTableComponents

'' Do some stuff

Next

End Sub
I have Another Form (eg: DisplayForm) which inherits from the BaseForm.
It adds (eg) a Label & a ImageList.

a.. If I look at the ControlsCollection (HashTableControls) I "find" a Button & a Label (Which is correct)
b.. If I look at the ComponentsCollection (HashTableComponents) I only "find" a Timer (Which is wrong: it should also contain the ImageList)
What is wrong with this approach?

TIA,

Michael

推荐答案

嗨查理,


组件在设计时添加(对派生形式)。

它们在继承表格上

代码不在构造函数中调用(sub = private - HashTable =

Public)但在BaseForm(DLL)中


注意:我添加了BaseForm.vb。在主项目中能够

在继承选择器中选择它(我还没有找到如何在继承选择器中显示

''baseform.dll''没有这个BaseForm.vb


所以我有:


°System.Windows.Forms.Form

°添加(很多)属性,事件,方法,...

°编译为类(baseform.dll)


°A Main-Project

°参考baseform.dll

°创建一个BaseForm.vb(从baseform.dll继承)

°全部主要项目的形式继承自BaseForm.vb


有趣的是,这适用于所有(类型)控件,但

(适用于一个原因或其他)不带组件。


对于组件来说,似乎是为BaseForm创建的集合

(带有实际的控件) BaseForm)和一个派生表格

(在设计中添加了组件-ti我想。)


问候,


Michael


PS:我使用vs.NET 2003 ENT。 Arch。

Hi Charlie,

The components are added in design-time (to the derived form).
They are on the Inherited form
The code is called not in the contructor (sub = private - HashTable =
Public) but in the BaseForm (DLL)

Note: I''ve added the "BaseForm.vb" in the "Main-Project" to be able to
select it in the inheritance-picker (I haven''t found how to display the
''baseform.dll'' in the inheritance-picker without this "BaseForm.vb"

so I have:

° System.Windows.Forms.Form
° Added (a lot of) Properties, Events, Methods, ...
° Compiled to a Class (baseform.dll)

° A "Main-Project"
° Referenced baseform.dll
° Created a BaseForm.vb (Inherits from baseform.dll)
° All forms of the main project inherit from that BaseForm.vb

The funny thing is that this works perfect with all (types of) Controls, but
(for one reason or the other) NOT with Components.

It seems like for the Components the collection is Created for the BaseForm
(with the actual Controls on that BaseForm) and one for the Derived Form
(With the Components added in design-time).

Regards,

Michael

PS: I use vs.NET 2003 Ent. Arch.


你好查理,


组件是在设计时添加的(到派生形式) 。

它们在继承表格上

代码不在contructor中调用(sub = private - HashTable =

Public)但是在BaseForm(DLL)


注意:我添加了BaseForm.vb。在主项目中能够

在继承选择器中选择它(我还没有找到如何在继承选择器中显示

''baseform.dll''没有这个BaseForm.vb


所以我有:


°System.Windows.Forms.Form

°添加(很多)属性,事件,方法,...

°编译为类(baseform.dll)


°A Main-Project

°参考baseform.dll

°创建一个BaseForm.vb(从baseform.dll继承)

°全部主要项目的形式继承自BaseForm.vb


有趣的是,这适用于所有(类型)控件,但

(适用于一个原因或其他)不带组件。


对于组件来说,似乎是为BaseForm创建的集合

(带有实际的控件) BaseForm)和一个派生表格

(在设计中添加了组件-ti我想。)


问候,


Michael


PS:我使用vs.NET 2003 ENT。 Arch。

Hi Charlie,

The components are added in design-time (to the derived form).
They are on the Inherited form
The code is called not in the contructor (sub = private - HashTable =
Public) but in the BaseForm (DLL)

Note: I''ve added the "BaseForm.vb" in the "Main-Project" to be able to
select it in the inheritance-picker (I haven''t found how to display the
''baseform.dll'' in the inheritance-picker without this "BaseForm.vb"

so I have:

° System.Windows.Forms.Form
° Added (a lot of) Properties, Events, Methods, ...
° Compiled to a Class (baseform.dll)

° A "Main-Project"
° Referenced baseform.dll
° Created a BaseForm.vb (Inherits from baseform.dll)
° All forms of the main project inherit from that BaseForm.vb

The funny thing is that this works perfect with all (types of) Controls, but
(for one reason or the other) NOT with Components.

It seems like for the Components the collection is Created for the BaseForm
(with the actual Controls on that BaseForm) and one for the Derived Form
(With the Components added in design-time).

Regards,

Michael

PS: I use vs.NET 2003 Ent. Arch.


嗨迈克尔,


根据我的理解,你循环完成Control系列和

BaseForm构造函数中的组件集合。当你使用

继承的表单DisplayForm时,你会发现BaseForm可以循环所有的

控件集合(甚至是DisplayForm的控件),但它找不到

DisplayForm的组件。


========================== ====

实际上,这是一种预期的行为。让我向你解释一下。


在你的BaseForm中,你像这样循环你的控件集合:

Private Sub ScanControls()

Dim [Control] As System.Windows.Forms.Control

For Each [Control] In Me.Controls

.......

下一页

End Sub


控制是BaseForm类的属性,而DisplayForm没有覆盖此属性,因此BaseForm和DisplayForm将共享相同的

属性。也就是说:DisplayForm中的Controls属性与

BaseForm相同。

因此在DisplayForm中,当您将Label拖到窗体上时,Label将是

添加到控件中,然后以父窗体(BaseForm)访问时,它可以

获得Label控件。


对于组件集合,它以私有变量

组件形式引用。子表单(DisplayForm)和父表单(BaseForm)每个都有一个

" components"他们自己的。他们没有共享一个副本。

所以当你将ImageList拖到子窗体(DisplayForm)时,只有

DisplayForm的组件变量被更改了。而BaseForm

ScanComponents方法将遍历BaseForm的组件变量。它是
看不到ImageList。


希望我澄清了它。

========= =========================

感谢您的耐心与合作。如果您有任何疑问或

担忧,请随时将其发布在小组中。我正准备等待

的帮助。


祝你好运,

Jeffrey Tan

Microsoft在线合作伙伴支持

安全! - www.microsoft.com/security

此帖子按原样提供没有保证,也没有赋予任何权利。

Hi Michael,

Based on my understanding, you loop through the Control collection and
component collection in your BaseForm constructor. When you are using your
inherited form DisplayForm, you find that the BaseForm can loop all the
control collection(even the control of DisplayForm), but it can not find
the components of DisplayForm.

==============================
Actually, this is an expected behavior. Let me explain it to you.

In your BaseForm, you loop through your control collection like this:
Private Sub ScanControls()
Dim [Control] As System.Windows.Forms.Control
For Each [Control] In Me.Controls
.......
Next
End Sub

The "Controls" is a property of BaseForm class, while DisplayForm did not
override this property, so BaseForm and DisplayForm will share the same
property. That is: the Controls property in DisplayForm is the same one of
BaseForm.
So in DisplayForm, when you drag Label onto the form, the Label will be
added into the "Controls", then when visit in parent form(BaseForm), it can
get the Label control.

While for component collection, it is referred in form''s private variable
"components". Child form(DisplayForm) and parent form(BaseForm) each has a
"components" of their own. They did not share one copy.
So when you drag ImageList to the Child form(DisplayForm), only
DisplayForm''s components variable was changed. While the BaseForm
ScanComponents method will loop through BaseForm''s components variable. It
can not see the ImageList.

Hope I have clarified it.
==================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


这篇关于形式'生命周期 - 继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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