2015-05-05 65 views
0

使用delphi XE7。我只想將框架加載到面板中,然後單擊按鈕在運行時查詢框架內標籤的標題屬性。我似乎無法得到它的工作。請告知。謝謝。Delphi:將框架嵌入面板並查詢框架內標籤的屬性

是Panel2在1單元

unit Unit1; 
interface 

uses 
Winapi.Windows, Winapi.Messages, System.SysUtils, 
System.Variants, System.Classes, Vcl.Graphics, 
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls; 

type 
TForm1 = class(TForm) 
Button4: TButton; 
Panel2: TPanel; 
procedure Button4Click(Sender: TObject); 
procedure FormCreate(Sender: TObject); 

private 
    { Private declarations } 
public 
    { Public declarations } 
end; 

var 
    Form1: TForm1; 
implementation 
{$R *.dfm} 

uses Unit3{TFrame3}; 

procedure TForm1.Button4Click(Sender: TObject); 
begin 
    ShowMessage(Frame3.Label4.caption); 
end; 

procedure TForm1.FormCreate(Sender: TObject); 
begin 
    Frame3.parent:=Panel2; 
end; 
end. 

幀3在本單位

unit Unit3; 
interface 

uses 
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, 
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, 
Vcl.StdCtrls; 

type 
TFrame3 = class(TFrame) 
Label4: TLabel; 
private 
public 
    constructor Create(TheOwner: TComponent); override; 
    destructor Destroy; override; 
end; 

var 
    Frame3: TFrame3; 

implementation 

{$R *.dfm} 

constructor TFrame3.Create(TheOwner: TComponent); 
begin 
    inherited Create(TheOwner); 
end; 

destructor TFrame3.Destroy; 
begin 
    inherited Destroy; 
end; 
end. 

當編譯好的,但運行到錯誤 - 與信息的訪問「第一次機會異常在$ 005E098C Exception類$ 0000005。處理Project1.exe(8416)「

+0

您需要實例幀3你TForm1.FormCreate引用它之前。如果你在該行放置了一個斷點,你會看到Frame3 = nil,因此你的訪問衝突 – Jason

+0

謝謝。我剛剛在這裏更新。我添加「Frame3.parent :: = Panel2」。我如何啓動它? – user1739825

+3

像任何其他類一樣。 Frame3:= TFrame3.Create(Self);除非Frame3將被Form1以外的其他應用程序所使用,否則您應該從Unit3中刪除全局的Frame3變量,並將其聲明爲TForm1中的私有字段。 – Jason

回答

1

您需要先創建幀的實例,然後才能在其中引用表單1.

設置你的TForm1.FormCreate事件是:

procedure TForm1.FormCreate(Sender: TObject); 
begin 
    Frame3 := TFrame3.Create(Self); 
    Frame3.Align := alClient; 
    Frame3.Parent := Panel2; 
end;