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?
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.
They're both derived from control class and both properties access value through same met
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With