创建自定义控件.我必须给他们起个名字吗? [英] Creating custom controls. Do I have to give them a name?
问题描述
比方说,我从TPanel创建并安装了自己的控件.它仅包含一个按钮:
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?
这个问题从dummzeuch在这里发表的评论开始:
This question started from a comment posted by dummzeuch here:
这种情况发生在您的标签没有名字的时候.IDE添加仅当控件具有名称时才向类声明(否则如何应该声明吗?).
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?).
推荐答案
否,您无需命名子组件.
No, you do not need to name sub components.
但是,如果您打算将对子组件的访问权限公开为在DFM中流式传输的属性,则确实需要标记子组件,至少:
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;
这篇关于创建自定义控件.我必须给他们起个名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!