E
eaglepsm
Unregistered / Unconfirmed
GUEST, unregistred user!
假设TFrame2中有一个Label1,窗体Form1中有一个PageControl,一个LabelMain,现在动态创建TTabSheet,并在里面动态创建TFrame2,即每个TabSheet都包含一个TFrame2,现在的问题是,如何在切换页面时用LabelMain显示TFrame中的Label中的内容?
//////////////////Unit3.pas
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls,unit2;
type
TForm3 = class(TForm)
Button1: TButton;
PageControl1: TPageControl;
LabelMain: TLabel;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure PageControl1Change(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
t,t1:TFrame2;
mysheet:TTabsheet;
pt,pt1:^TFrame2; //调试用
implementation
{$R *.dfm}
procedure TForm3.Button1Click(Sender: TObject);
begin
MySheet:=TTabsheet.Create(PageControl1);
MySheet.Parent:=PageControl1;
MySheet.PageControl:=PageControl1;
t:=TFrame2.Create(self.PageControl1.pages[0]);
pt:=@t;
MySheet.Tag:=integer(@t);
t.Parent:=Self.PageControl1.pages[0];
t.Align:=alclient;
t.Label1.Caption:='1';
MySheet:=TTabsheet.Create(PageControl1);
MySheet.Parent:=PageControl1;
MySheet.PageControl:=PageControl1;
t1:=Tframe2.create(Self.PageControl1.pages[1]);
pt1:=@t1;
MySheet.Tag:=integer(@t1);
t1.parent:=Self.pagecontrol1.pages[1];
t1.align:=alclient;
t1.Label1.Caption:='2';
end;
procedure TForm3.PageControl1Change(Sender: TObject);
var
str:string;
s:TFrame2;
begin
str:=TFrame2(PageControl1.ActivePage.Tag).Label1.Caption; //不知为什么得到的是空的
s:=TFrame2(PageControl1.ActivePage.Tag); //这个办法也不行
LabelMain.Caption:=s.Label1.Caption;
end;
procedure TForm3.Button2Click(Sender: TObject);
begin
//(t.Label1.Caption,t1.Label1.caption也可以访问)
pt.Label1.Caption:='hello t'; //这样访问当然可以,但不符合我的要求(切换页面时自动显示)
pt1.Label1.caption:='hello t1';
end;
end.
///////////////////////Unit3.dfm
object Form3: TForm3
Left = 189
Top = 210
Width = 431
Height = 281
Caption = 'Form3'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object LabelMain: TLabel
Left = 48
Top = 8
Width = 49
Height = 13
Caption = 'LabelMain'
end
object Button1: TButton
Left = 320
Top = 32
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object PageControl1: TPageControl
Left = 40
Top = 40
Width = 257
Height = 169
TabOrder = 1
OnChange = PageControl1Change
end
object Button2: TButton
Left = 320
Top = 72
Width = 75
Height = 25
Caption = 'Button2'
TabOrder = 2
OnClick = Button2Click
end
end
////////////////Unit2.pas
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TFrame2 = class(TFrame)
Label1: TLabel;
private
{ Private declarations }
public
{ Public declarations }
end;
implementation
uses Unit1;
{$R *.dfm}
end.
//////// Unit2.dfm
object Frame2: TFrame2
Left = 0
Top = 0
Width = 235
Height = 132
TabOrder = 0
object Label1: TLabel
Left = 104
Top = 48
Width = 32
Height = 13
Caption = 'Label1'
end
end
//////////////////Unit3.pas
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls,unit2;
type
TForm3 = class(TForm)
Button1: TButton;
PageControl1: TPageControl;
LabelMain: TLabel;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure PageControl1Change(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
t,t1:TFrame2;
mysheet:TTabsheet;
pt,pt1:^TFrame2; //调试用
implementation
{$R *.dfm}
procedure TForm3.Button1Click(Sender: TObject);
begin
MySheet:=TTabsheet.Create(PageControl1);
MySheet.Parent:=PageControl1;
MySheet.PageControl:=PageControl1;
t:=TFrame2.Create(self.PageControl1.pages[0]);
pt:=@t;
MySheet.Tag:=integer(@t);
t.Parent:=Self.PageControl1.pages[0];
t.Align:=alclient;
t.Label1.Caption:='1';
MySheet:=TTabsheet.Create(PageControl1);
MySheet.Parent:=PageControl1;
MySheet.PageControl:=PageControl1;
t1:=Tframe2.create(Self.PageControl1.pages[1]);
pt1:=@t1;
MySheet.Tag:=integer(@t1);
t1.parent:=Self.pagecontrol1.pages[1];
t1.align:=alclient;
t1.Label1.Caption:='2';
end;
procedure TForm3.PageControl1Change(Sender: TObject);
var
str:string;
s:TFrame2;
begin
str:=TFrame2(PageControl1.ActivePage.Tag).Label1.Caption; //不知为什么得到的是空的
s:=TFrame2(PageControl1.ActivePage.Tag); //这个办法也不行
LabelMain.Caption:=s.Label1.Caption;
end;
procedure TForm3.Button2Click(Sender: TObject);
begin
//(t.Label1.Caption,t1.Label1.caption也可以访问)
pt.Label1.Caption:='hello t'; //这样访问当然可以,但不符合我的要求(切换页面时自动显示)
pt1.Label1.caption:='hello t1';
end;
end.
///////////////////////Unit3.dfm
object Form3: TForm3
Left = 189
Top = 210
Width = 431
Height = 281
Caption = 'Form3'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object LabelMain: TLabel
Left = 48
Top = 8
Width = 49
Height = 13
Caption = 'LabelMain'
end
object Button1: TButton
Left = 320
Top = 32
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object PageControl1: TPageControl
Left = 40
Top = 40
Width = 257
Height = 169
TabOrder = 1
OnChange = PageControl1Change
end
object Button2: TButton
Left = 320
Top = 72
Width = 75
Height = 25
Caption = 'Button2'
TabOrder = 2
OnClick = Button2Click
end
end
////////////////Unit2.pas
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TFrame2 = class(TFrame)
Label1: TLabel;
private
{ Private declarations }
public
{ Public declarations }
end;
implementation
uses Unit1;
{$R *.dfm}
end.
//////// Unit2.dfm
object Frame2: TFrame2
Left = 0
Top = 0
Width = 235
Height = 132
TabOrder = 0
object Label1: TLabel
Left = 104
Top = 48
Width = 32
Height = 13
Caption = 'Label1'
end
end