如何访问动态创建的TFrame的成员?(50分)

  • 主题发起人 主题发起人 eaglepsm
  • 开始时间 开始时间
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
 
MySheet.Tag:=integer(@t); -> MySheet.Tag:=integer(t);
 
测试成功,但这样改的道理是什么?MySheet.Tag应该是存放TFrame的地址呀?
另外,这里是用两个变量来创建2个TabSheet,实际上不会这样,更多的情况是使用一个变量,但创建的parent不同,下面这个例子同上面的文件共同构成一个项目,直接添加进取即可。能够运行,但切换页面时会出现异常,不知是为什么。
////////unit1.pas
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, RzTabs, StdCtrls, ComCtrls, Unit2;

type
TForm1 = class(TForm)
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet3: TTabSheet;
ButtonAdd: TButton;
Label2: TLabel;
LabelActivePageIndex: TLabel;
ButtonshowForm2: TButton;
procedure ButtonAddClick(Sender: TObject);
procedure PageControl1Change(Sender: TObject);
procedure ButtonshowForm2Click(Sender: TObject);
private
{ Private declarations }
MySheet:TTabSheet;
MyFrame:TFrame2;
pMyFrame:^TFrame2;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses Unit3;

{$R *.dfm}

procedure TForm1.ButtonAddClick(Sender: TObject);
var
P:TPageControl;
pt:^TFrame2;
t:TFrame;
tab:TTabSheet;
begin
tab:=TTabSheet.Create(PageControl1);
MySheet:=tab;
MySheet.PageControl:=PageControl1;
MySheet.Caption:=inttostr(PageControl1.PageCount);
t:=nil;
t:=TFrame2.Create(PageControl1.Pages[PageControl1.PageCount-1]);
pt:=@t;
MyFrame:=TFrame2(t);
MyFrame.Parent:=MySheet;
MySheet.Tag:=integer(t);
pt.Label2.Caption:=inttostr(MySheet.Tag);
MyFrame.Label1.Caption:=PageControl1.ActivePage.Caption;
MyFrame.Show;
end;

procedure TForm1.PageControl1Change(Sender: TObject);
Var
k:integer;
s:^TFrame2;
begin
LabelActivePageIndex.Caption:=inttostr(PageControl1.ActivePageIndex);
s:=Pointer(TFrame2(PageControl1.ActivePage.Tag)); //这个办法也不行 必须用指针
Label2.Caption:=s.Label1.Caption;
end;

procedure TForm1.ButtonshowForm2Click(Sender: TObject);
begin
Form3.show;
end;

end.
////////////////unit1.dfm
object Form1: TForm1
Left = 192
Top = 107
Width = 639
Height = 411
Caption = 'Form1'
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 Label2: TLabel
Left = 88
Top = 48
Width = 32
Height = 13
Caption = 'Label2'
end
object LabelActivePageIndex: TLabel
Left = 184
Top = 48
Width = 107
Height = 13
Caption = 'LabelActivePageIndex'
end
object PageControl1: TPageControl
Left = 24
Top = 144
Width = 385
Height = 217
ActivePage = TabSheet1
TabOrder = 0
OnChange = PageControl1Change
object TabSheet1: TTabSheet
Caption = 'TabSheet1'
end
object TabSheet3: TTabSheet
Caption = 'TabSheet3'
ImageIndex = 1
end
end
object ButtonAdd: TButton
Left = 440
Top = 144
Width = 75
Height = 25
Caption = 'add'
TabOrder = 1
OnClick = ButtonAddClick
end
object ButtonshowForm2: TButton
Left = 440
Top = 192
Width = 137
Height = 25
Caption = 'ButtonshowForm2'
TabOrder = 2
OnClick = ButtonshowForm2Click
end
end
 
建议楼主找本Object Pascal的书看看,尤其注意“对象指针”的概念;DelphiBox有这方面的资料。
 
接受答案了.
 

Similar threads

后退
顶部