【问题标题】:Creating Objects Dynamically on a frame在框架上动态创建对象
【发布时间】:2013-06-04 14:25:09
【问题描述】:

我有两种情况。一种有效,一种无效。第一个(有效的)包含一个直接位于表单上的滚动框,当按下按钮时它会执行以下代码:

procedure TForm1.Button2Click(Sender: TObject); 
begin
 DrawPanel;
end;

procedure TForm1.DrawPanel;
begin
BuildPanel; //Resides on a seperate unit code pasted below

TestPanel.Height := 40;
TestPanel.Width := 100;
TestPanel.Left := Trunc(ScrollBox1.Width / 2) - Trunc(TestPanel.Width / 2);
TestPanel.Top := Trunc(ScrollBox1.Height / 2) - Trunc(TestPanel.Height / 2);
TestPanel.Visible := True;
TestPanel.Parent := ScrollBox1;

end;

unit Unit3;

interface

uses ExtCtrls;

Var
 TestPanel : Tpanel;

Procedure BuildPanel; 

implementation

procedure BuildPanel;
begin
 TestPanel := TPanel.Create(Nil);
end;

end.

代码是相同的,只是在第二种情况下有一点不同。滚动框位于添加到模板调色板的框架上,然后下拉到表单上。按钮点击调用:

procedure TForm1.Button1Click(Sender: TObject);
begin
TestFrame.DrawPanel;
end;

procedure TTestFrame.DrawPanel;
begin
 BuildPanel; //Still points to the unit3 code above

   TestPanel.Height := 40;
   TestPanel.Width := 100;
   TestPanel.Left := Trunc(ScrollBox1.Width / 2) - Trunc(TestPanel.Width / 2);
   TestPanel.Top := Trunc(ScrollBox1.Height / 2) - Trunc(TestPanel.Height / 2);
   TestPanel.Visible := True;
   TestPanel.Parent := ScrollBox1;
end; 

但是,在运行时触发时,面板不会显示在框架上的滚动框中。我不确定为什么,有人可以帮忙吗?我希望我的问题足够具体,如果有任何不清楚的地方,请告诉我。提前致谢。

这是所有的代码......希望它更清楚:

//This is the form 
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Unit2, Unit3;

type
 TForm1 = class(TForm)
 Button1: TButton;
 TTestFrame1: TTestFrame;
 ScrollBox1: TScrollBox;
 Button2: TButton;
 procedure Button1Click(Sender: TObject);
 procedure FormShow(Sender: TObject);
 procedure FormClose(Sender: TObject; var Action: TCloseAction);
 procedure Button2Click(Sender: TObject);
private
 { Private declarations }
 TestFrame: TTestFrame;
 Procedure DrawPanel;
public
 { Public declarations }
end;

var
 Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
 TestFrame.DrawPanel;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
 DrawPanel;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 TestFrame.Free;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
 TestFrame := TTestFrame.Create(Form1);
end;

procedure TForm1.DrawPanel;
begin
 BuildPanel;

 TestPanel.Height := 40;
 TestPanel.Width := 100;
 TestPanel.Left := Trunc(ScrollBox1.Width / 2) - Trunc(TestPanel.Width / 2);
 TestPanel.Top := Trunc(ScrollBox1.Height / 2) - Trunc(TestPanel.Height / 2);
 TestPanel.Visible := True;
 TestPanel.Parent := ScrollBox1;

end;

end.

//This is the frame
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, Unit3;

type
 TTestFrame = class(TFrame)
 ScrollBox1: TScrollBox;
private
 { Private declarations }
public
 { Public declarations }
  Procedure DrawPanel;
end;

implementation

{$R *.dfm}

{ TTestFrame }

procedure TTestFrame.DrawPanel;
begin
 BuildPanel;

 TestPanel.Height := 40;
 TestPanel.Width := 100;
 TestPanel.Left := Trunc(ScrollBox1.Width / 2) - Trunc(TestPanel.Width / 2);
 TestPanel.Top := Trunc(ScrollBox1.Height / 2) - Trunc(TestPanel.Height / 2);
 TestPanel.Visible := True;
 TestPanel.Parent := ScrollBox1;

end;
end.

//This is the unit that mocks my data structure
//In reality it creates an Array of Tpanel that is part of a class.                                                                                                         
unit Unit3;

interface

uses ExtCtrls;

Var
 TestPanel : Tpanel;

Procedure BuildPanel;

implementation

procedure BuildPanel;
begin
 TestPanel := TPanel.Create(Nil);
end;

end.

【问题讨论】:

  • 代码分散在各处,有点难以理解。我确实想知道为什么您觉得需要使用全局变量。一个完整的程序的任何机会?一个单元中的所有代码?
  • 这是我实际所做的模拟。这就是我使用全局变量的原因,我只是在测试理论。我可以发布所有代码,但如前所述,它位于表单、框架和处理数据结构的单独单元之间。如果有帮助的话,我会完整地发布代码。
  • 您正在创建 TestFrame,但没有分配父级。
  • 就是这样!非常感谢你!
  • 你可以用trunc(scrollbox.width / 2)代替scrollbox.width div 2

标签: delphi


【解决方案1】:

您只是忘记将parent 分配给您动态创建的TestFrame。

【讨论】:

  • 再次感谢 bummi,我知道我必须过度关注一些事情。
猜你喜欢
  • 1970-01-01
  • 2016-04-03
  • 2012-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-11
  • 1970-01-01
相关资源
最近更新 更多