Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 393
Jerry Dodge Avatar asked Dec 18 '11 23:12

Jerry Dodge


2 Answers

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.

like image 119
Andreas Rejbrand Avatar answered Oct 07 '22 16:10

Andreas Rejbrand


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

like image 41
APZ28 Avatar answered Oct 07 '22 17:10

APZ28