不知何故,意外地混合 TEdit.Text 和 TLabel.Caption 毫无例外地工作吗?
我正在动态创建多种不同类型的控件并将它们存储在后台的列表中。其中两个控件是 TEdit 和 TLabel。这些控件都是 TPanel 控件的子控件。
好吧,在写入 TEdit.Text 和 TLabel.Caption 属性时,我在从 TPanel.Controls 读取(从错误的索引中获取它们)时不小心将 TEdit 与 TLabel 混淆了。不知何故,它甚至没有引发任何异常。
它执行类似 TLabel(SomeEditControl).Caption:= 'This is a label control';
和 TEdit(SomeLabelControl).Text:= 'This is an edit control';
> 它将 TEdit.Text 数据放入 TLabel.Caption 属性中,并将 TLabel.Caption 数据放入 TEdit.Text 属性中。我很困惑这并没有引发异常...我唯一能猜测的是 TEdit.Text 和 TLabel.Caption 属性恰好在控件类之间使用相同的内存地址。
为什么这不会造成访问冲突?
I'm working with dynamically creating multiple different types of controls and storing them in a list in the background. Two of such controls are a TEdit and a TLabel. These controls are all sub-controls of a TPanel control.
Well I accidentally mixed up the TEdit with the TLabel when reading from TPanel.Controls (got them from the wrong indexes) when writing to the TEdit.Text and TLabel.Caption properties. Somehow, it didn't even raise any exception.
It does something like TLabel(SomeEditControl).Caption:= 'This is a label control';
and TEdit(SomeLabelControl).Text:= 'This is an edit control';
And it puts the TEdit.Text data in the TLabel.Caption property, and the TLabel.Caption data in the TEdit.Text property. I'm puzzled that this didn't raise an exception... The only thing I can guess is that the TEdit.Text and TLabel.Caption properties just so happen to use the same memory address between the control classes.
Why wouldn't this give an access violation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TEdit.Text
来自TControl.Text
,TLabel.Caption
来自TControl.Caption
。但看看TControl
的声明:具有一些与之关联的文本的控件通过
SetText
和GetText
方法对其进行管理。根据控件的类型,它们与Caption
或Text
属性相关联。从文档,The
TEdit.Text
comes fromTControl.Text
, and theTLabel.Caption
comes fromTControl.Caption
. But look at the declaration ofTControl
:A control that has some text associated with it manages it via the
SetText
andGetText
methods. Depending on the type of control, these are associated either with theCaption
or theText
property. From the documentation,它们都派生自控件类,并且两个属性都通过相同的接口访问值
They're both derived from control class and both properties access value through same met