0

Let's say I create (from TPanel) and install my own control. It contains only one button:

constructor TMyPanel.Create(aOwner: TComponent);
begin
 inherited Create(aOwner);

 btn:= TButton.Create(Self);
 btn.Parent:= Self;   
 btn.Name:= 'xxx';      <-------- mandatory?
end;

Is it mandatory to give a name to that panel?


This question started from a comment posted by dummzeuch here:

This happens when your label doesn't have a name. The IDE adds declarations to the class only if the control has got a name (how else should it declare it?).

What does a EClassNotFound raised at runtime really mean when the class in question is there at compile and link time, and there explicitly in code?

(I am specifically refer to that comment only)

Gabriel
  • 20,797
  • 27
  • 159
  • 293
  • 1
    You are mixing apples and oranges. Linked question was about streaming from dfm, and here you are creating control in code. No, you don't need a name, but you might want to set it as sub component. – Dalija Prasnikar Nov 13 '18 at 10:30
  • 1
    The IDE needs components to have a name, but otherwise no. So if the IDE does not *directly* access any of the properties of the subcomponent then it does not need a name. This does not stop the properties of the component being set by the parent, for example via a property of the parent that is visible to the IDE. – Dsm Nov 13 '18 at 10:56
  • @DalijaPrasnikar - Nope. I was not speaking about that question. I was speaking about one VERY specific comment (that was also not answering that question). – Gabriel Nov 13 '18 at 11:11
  • @Dsm - thanks. I was not naming my sub-controls. And as you said, it was working ok. But that comment got me worrying: "what if I was doing it wrong all the time" ? If you post this as answer, I will accept it. – Gabriel Nov 13 '18 at 11:12
  • @DalijaPrasnikar - the control i show here will be IN THE END also streamed when I drop it on my form and save the project/the form. anyway my question is is about assigning a name to a sub-control. – Gabriel Nov 13 '18 at 11:19
  • 1
    Comment posted as answer, as requested – Dsm Nov 13 '18 at 13:51

2 Answers2

3

No, you do not need to name sub components.

However, if you intent on exposing access to a sub componenet as a property that gets streamed in a DFM, you do need to mark the sub component, at least:

constructor TMyPanel.Create(aOwner: TComponent);
begin
  inherited Create(aOwner);
  btn := TButton.Create(Self);
  btn.Parent := Self;
  btn.SetSubComponent(True); // <-- ADD THIS
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
2

The IDE needs components to have a name, but otherwise no. So if the IDE does not directly access any of the properties of the subcomponent then it does not need a name. This does not stop the properties of the component being set by the parent, for example via a property of the parent that is visible to the IDE.

Dsm
  • 5,870
  • 20
  • 24