使用Controls属性时是否有任何通知? [英] Is there any notice when using Controls property?
问题描述
我使用Tag属性存储控件的ArrayList,它运行良好.但是我尝试使用Controls属性来代替,通过它我可以在不使用ArrayList的情况下向其中添加控件,我想使用Tag属性来存储其他控件.更改后,我无法使我的应用程序像以前一样运行.我认为Controls属性可能需要学习一些特殊说明.你能告诉我它是否有明显的东西!
另外,我怀疑在某些情况下Controls.Add(某些控件)无法成功完成!?
非常感谢!
I used Tag property to store an ArrayList of controls, it worked well. But I tried using Controls property instead, with it I can add a control to without needing an ArrayList, I wanted to use Tag property to store others. After changing I can''t make my application run as before. I think Controls property may have some special notes to learn. Could you please tell me if it has something noticeable!
Plus, I am suspecting that there is some case that Controls.Add(some control) can''t be done successfully!?
Thank you so much!
推荐答案
您可以将任何控件添加到表单或容器的Controls
集合中.
从使用Tag
属性/ArrayList
转换为使用更合适的集合(Controls
)时,您可能会犯一些严重错误.
You can add any control to theControls
collection in a form or container.
Chances are that you made some critical errors when you changed over from use theTag
property/ArrayList
to using the more appropriate collection (Controls
).
没有特别说明"!您正在尝试滥用System.Windows.Forms
的基本属性.
属性System.Windows.Forms.Constrol.Controls
具有最基本的副作用:添加的控件实际上出现在UI上(这是在运行时向UI添加控件的一种方式).添加的控件成为您正在使用的Controls
属性的控件的子级;并且添加后,每个添加的控件的属性Parent
都会相应地修改.
您可以采用另一种方法:如果您修改任何控件的属性Parent
,它将立即跳转到另一个父控件!如果Parent
的先前值为null
,它将使控件出现在屏幕上.新的父控件的Controls属性的子控件列表将自动进行相应的修改.
相反,System.Windows.Forms.Constrol.Tag
不执行任何操作.它仅用作任何对象的容器,因此非常有用.通常,它用于存储一些要在某些事件处理过程中查找的数据.但这并不一定是数据.它甚至可以是对象,例如委托实例或诸如此类的复杂对象:-).
—SA
There is no "special notes"! You''re trying to misuse properties fundamental toSystem.Windows.Forms
.
The propertySystem.Windows.Forms.Constrol.Controls
has most fundamental side effect: the added controls actually appear on the UI (this is a way to add controls to the UI during run time). The added controls become children of the control whichControls
property you''re using; and the propertyParent
of each added control is modified accordingly as a result of adding.
You can do it the other way around: if you modify the propertyParent
of any control, it will immediately jump to a different parent control! If previous value ofParent
wasnull
, it will make a control appearing on screen. Automatically, the list of child controls of new parent''s Controls property will be modified accordingly.
In contrast,System.Windows.Forms.Constrol.Tag
does nothing. It is used just as a container for any objects, very useful as such. Typically, it is used to store some data to be looked up during some event handling. But it does not have to be data; it can be even object, for example, the delegate instance or something as sophisticated :-).
—SA
这篇关于使用Controls属性时是否有任何通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!