두 가지 시나리오가 있습니다. 하나는 작동하지만 하나는 작동하지 않습니다.프레임에 동적으로 개체 생성
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.
코드가 두 번째 시나리오에서 작은 차이를 제외하고는 동일하다 : 제 (작동 하나)는 스크롤 박스는 버튼이 가압 될 때이 코드를 실행한다는 형태로 직접 앉아 invloves. 스크롤 상자는 템플릿 팔레트에 추가 된 후 폼에 드롭 된 프레임에 위치합니다. 버튼 클릭은 다음과 같이 호출합니다.
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.
여기 저기에 흩어져있는 모든 코드를 따르기가 쉽지 않습니다. 나는 왜 당신이 글로벌 변수를 사용할 필요성을 느끼는지 궁금해. 완전한 프로그램의 기회? 단일 코드의 모든 코드? –
이것은 실제로 내가하고있는 것의 모형이다. 그래서 전역 변수를 사용하고 있습니다. 단순히 이론을 테스트하는 것입니다. 모든 코드를 게시 할 수 있지만 언급 한대로 양식, 프레임 및 데이터 구조를 처리하는 별도의 단위 사이에 있습니다. 코드가 도움이된다면 코드를 게시하십시오. – CodeMonkey
당신은 TestFrame을 만들고 있지만 부모는 할당하지 않습니다. – bummi