不知何故,意外地混合 TEdit.Text 和 TLabel.Caption 毫无例外地工作吗?

发布于 2024-12-21 17:50:17 字数 543 浏览 5 评论 0原文

我正在动态创建多种不同类型的控件并将它们存储在后台的列表中。其中两个控件是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事与诗 2024-12-28 17:50:17

TEdit.Text 来自TControl.TextTLabel.Caption 来自TControl.Caption。但看看 TControl 的声明:

...
property Caption: TCaption read GetText write SetText stored IsCaptionStored;
...
property Text: TCaption read GetText write SetText;
...

具有一些与之关联的文本的控件通过 SetTextGetText 方法对其进行管理。根据控件的类型,它们与 CaptionText 属性相关联。从文档

注意:显示文本的控件使用 Caption 属性或
Text 属性指定文本值。使用的属性
取决于控制类型。一般来说,Caption用于文本
显示为窗口标题或标签,而 Text 用于文本
显示为控件的内容。

The TEdit.Text comes from TControl.Text, and the TLabel.Caption comes from TControl.Caption. But look at the declaration of TControl:

...
property Caption: TCaption read GetText write SetText stored IsCaptionStored;
...
property Text: TCaption read GetText write SetText;
...

A control that has some text associated with it manages it via the SetText and GetText methods. Depending on the type of control, these are associated either with the Caption or the Text property. From the documentation,

Note: Controls that display text use either the Caption property or
the Text property to specify the text value. The property that is used
depends on the type of control. In general, Caption is used for text
that appears as a window title or label, while Text is used for text
that appears as the content of a control.

妄断弥空 2024-12-28 17:50:17

它们都派生自控件类,并且两个属性都通过相同的接口访问值

They're both derived from control class and both properties access value through same met

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文