Somehow accidentally mixing TEdit.Text and TLabel.Caption works without exception?
Asked Answered
B

2

5

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?

Boundary answered 18/12, 2011 at 23:18 Comment(2)
They all descend from TControl which has a WindowText property. ( Just guessing...)Broadnax
@AndreasRejbrand That's a good possibility, TEdit.Text and TLabel.Caption I presume do translate to the WindowTextBoundary
B
9

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.

Broadnax answered 18/12, 2011 at 23:28 Comment(7)
There's no FText, it's WM_GETTEXT, +1Wilterdink
Or WM_SETTEXT as a setter for those properties. +1ed, you were faster ;)Chi
@David: Actually, there is an FText. Have a look at SetText. (But still, you are right. The main part seems to be based on these messages.)Broadnax
That's probably why they removed Caption in FireMonkey. Every control now only has a Text property (except for forms, to keep it simple).Wack
FText seemed to be only used for CLR when I read GetText, SetText etc.Wilterdink
@David: Yes, I revised my answer.Broadnax
@Wack in FireMonkey, nothing was 'removed' or 'added' based on VCL. Instead, FireMonkey is an entirely new raw system, not based on anything but OOP. But I understand your point. There are a lot of 'differences' in FM, not necessarily 'changes' from VCL.Boundary
J
1

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

Jacobine answered 19/12, 2011 at 2:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.