加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 不知何故意外混合TEdit.Text和TLabel.Caption无异常地

发布时间:2020-12-15 04:17:09 所属栏目:大数据 来源:网络整理
导读:我正在动态创建多种不同类型的控件,并将它们存储在后台列表中.其中两个控件是TEdit和TLabel.这些控件都是TPanel控件的子控件. 当我写入TEdit.Text和TLabel.Caption属性时,从TPanel.Controls(从错误的索引中获取它们)读取时,我意外地将TEdit与TLabel混淆了.不
我正在动态创建多种不同类型的控件,并将它们存储在后台列表中.其中两个控件是TEdit和TLabel.这些控件都是TPanel控件的子控件.

当我写入TEdit.Text和TLabel.Caption属性时,从TPanel.Controls(从错误的索引中获取它们)读取时,我意外地将TEdit与TLabel混淆了.不知何故,它甚至没有引起任何例外.

它有点像TLabel(SomeEditControl).Caption:=’这是一个标签控件’;和TEdit(SomeLabelControl).Text:=’这是一个编辑控件’;它将TEdit.Text数据放在TLabel.Caption属性中,将TLabel.Caption数据放在TEdit.Text属性中.我很困惑,这没有引发异常…我唯一可以猜到的是,TEdit.Text和TLabel.Caption属性恰好恰好在控件类之间使用相同的内存地址.

为什么这不会导致访问冲突?

解决方法

TEdit.Text来自TControl.Text,TLabel.Caption来自TControl.Caption.但是看看TControl的声明:
...
property Caption: TCaption read GetText write SetText stored IsCaptionStored;
...
property Text: TCaption read GetText write SetText;
...

具有与之关联的文本的控件通过SetText和GetText方法对其进行管理.根据控件的类型,它们与Caption或Text属性相关联.从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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读