加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 为什么我的运行时创建的组件不出现在表单上?

发布时间:2020-12-15 09:20:26 所属栏目:大数据 来源:网络整理
导读:我正在测试来自此Q A Component Creation – Joining Components Together?的示例,以了解如何创建自定义/复合组件. 虽然示例中安装的组件可以拖动到表单,但我似乎无法在运行时创建它. procedure TForm1.Button1Click(Sender: TObject);varMyPanel2 : TMyPane
我正在测试来自此Q& A Component Creation – Joining Components Together?的示例,以了解如何创建自定义/复合组件.

虽然示例中安装的组件可以拖动到表单,但我似乎无法在运行时创建它.

procedure TForm1.Button1Click(Sender: TObject);
var
MyPanel2 : TMyPanel;
begin
MyPanel2 := TMyPanel.Create(Form1);
With MyPanel2 do
  begin
    Left := 10;
    Top := 10;
    Width := 400;
    Height := 400;
    Visible := True;
    Image.Picture.LoadFromFile('C:test.png');
  end;
end;

我尝试了自己和Form1作为所有者.使用面板和图像的属性.

只是不确定我做错了什么.没有错误,除非我忘记添加pngimage到我的用途.完成代码的步骤很好,运行时创建没有任何可视化.

解决方法

您需要在运行时代码中设置 Parent.

MyPanel2 := TMyPanel.Create(Self);
with MyPanel2 do
begin
  Parent := Self;//oops,you forgot to set this
  SetBounds(10,10,400,400);
  Image.Picture.LoadFromFile('C:test.png');
end;

您问题中的代码不会导致控件显示普通的香草TPanel,或实际上任何控件.

从documentation开始,我强调:

Specifies the parent of the control.

Use the Parent property to get or set the parent of the control. The
parent of a control is the control that contains it. For example,if
an application includes three radio buttons in a group box,the group
box is the parent of the three radio buttons,and the radio buttons
are the child controls of the group box.

To serve as a parent,a control must be an instance of a TWinControl
descendant.

When creating a new control at run time,assign a Parent property value for the new control. Usually,this is a form,panel,group box,or a control that is designed to contain another. Changing the parent of a control moves the control onscreen so that it is displayed within the new parent. When the parent control moves,the child moves with the parent.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读