I am creating a custom control derived from TCustomControl, for example:
type TMyCustomControl = class(TCustomControl) private FText: string; procedure SetText(const Value: string); protected procedure Paint; override; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; published property Text: string read FText write SetText; end;
Please note that the above is incomplete for this example to be short and simple.
Anyway, in my control, I have a Paint event that displays text (from the FText field) using Canvas.TextOut.
When my component is added to the Delphi form constructor (before the user can make changes to the component), I want TextOut to display the name Component-TButton, TCheckBox, TPanel, etc., are examples of this with their property header.
If I try to assign the name of my component to FText in the constructor, it will be empty, for example '' ;
constructor TMyCustomControl.Create(AOwner: TComponent); begin inherited Create(AOwner); FText := Name; //< empty string ShowMessage(Name); //< empty message box too end;
If I change FText := Name to FText := 'Name'; , it outputs the text to my Component, so I know that this is not a problem in the actual code, but obviously it outputs the "Name" and not the actual name of the component, for example MyCustomControl1, MyCustomControl2, etc.
So my question is: how can you get the name of your component from your constructor event?
user1175743
source share