分享

动态创建PageControl、TabSheet

 独孤求财 2012-04-01

var
  T : TTabSheet;
  P : TPageControl;
begin
   // Create the PageControl
   // need to reference the page control so we need a reference to it.

  P := TPageControl.Create(application);
  with P do begin
    Parent := Form1;  // set how controls it.
    Top := 30;
    Left := 30;
    Width := 200;
    Height := 150;
    end;  // with TPageControl

    // Create 3 pages
  T := TTabSheet.Create(P);
  with T do begin
    Visible := True;   // This is necessary or form does not repaint
                       // correctly
    Caption := 'Page 1';
    PageControl := P;  // Assign Tab to Page Control
    end;  // with

  T := TTabSheet.Create(P);
  with T do begin
    Visible := True;   // This is necessary or form does not repaint
                       // correctly
    Caption := 'Page 2';
    PageControl := P;  // Assign Tab to Page Control
    end;  // with

  T := TTabSheet.Create(P);
  with T do begin
    Visible := True;   // This is necessary or form does not repaint
                       // correctly
    Caption := 'Page 3';
    PageControl := P;  // Assign Tab to Page Control

    end;  // with

    // Create 3 buttons, 1 per page
  with tbutton.create(application) do begin
    Parent := P.Pages[0];  // Tell which page owns the Button
    Caption := 'Hello Page 1';
    Left := 0;
    Top := 0;
    end; // with

  with tbutton.create(application) do begin
    Parent := P.Pages[1];  // Tell which page owns the Button
    Caption := 'Hello Page 2';
    Left := 50;
    Top := 50;
    end; // with

  with tbutton.create(application) do begin

    Parent := P.Pages[2];  // Tell which page owns the Button
    Caption := 'Hello Page 3';
    Left := 100;
    Top :=  90;
    end; // with

    // This needs to be done or the Tab does not sync to the
    // correct page, initially.  Only if you have more then
    // one page.
  P.ActivePage := P.Pages[1];
  P.ActivePage := P.Pages[0];  // page to really show
end;
 
//以下动态创建EDIT控件
 

private

procedure EdtEnter(sender: TObject);

procedure EdtExit(sender: TObject);

end;

procedure Tfrm.EdtEnter(sender: TObject);
begin
TEdit(sender).Color:=clInfoBk;//当EDIT获得焦点时的颜色
end;

procedure Tfrm.EdtExit(sender: Tobject);
begin
TEdit(sender).Color:=clWindow;//失去焦点时的颜色
end;


//批量创建控件

var
i:integer;
begin
for i:=1 to 10 do
begin
   with  TEdit.Create(Self) do
   begin
     Name:='edt'+IntToStr(i);
     Parent:=self;
     Width:=75;
     Height:=24;
     Top:=i*32;//位置
     Left:=10;
     Text:='';
     OnEnter:=EdtEnter;
     OnExit:=EdtExit;
   end;
end;

//控件数组

var
i:integer;
edt:array[0..9] of TEdit;
begin
for i:=1 to 10 do
begin
   edt[i]:=TEdit.Create(Self);
   with edt[i] do
   begin
   Name:='edt'+IntTostr(i);
   Parent:=Form1;
   Width:=75;
   Height:=24;
   Top:=i*32;//位置
   Left:=10;
end;
end;

private

procedure EdtEnter(sender: TObject);

procedure EdtExit(sender: TObject);

end;

procedure Tfrm.EdtEnter(sender: TObject);
begin
TEdit(sender).Color:=clInfoBk;//当EDIT获得焦点时的颜色
end;

procedure Tfrm.EdtExit(sender: Tobject);
begin
TEdit(sender).Color:=clWindow;//失去焦点时的颜色
end;


//批量创建控件

var
i:integer;
begin
for i:=1 to 10 do
begin
   with  TEdit.Create(Self) do
   begin
     Name:='edt'+IntToStr(i);
     Parent:=self;
     Width:=75;
     Height:=24;
     Top:=i*32;//位置
     Left:=10;
     Text:='';
     OnEnter:=EdtEnter;
     OnExit:=EdtExit;
   end;
end;

//控件数组

var
i:integer;
edt:array[0..9] of TEdit;
begin
for i:=1 to 10 do
begin
   edt[i]:=TEdit.Create(Self);
   with edt[i] do
   begin
   Name:='edt'+IntTostr(i);
   Parent:=Form1;
   Width:=75;
   Height:=24;
   Top:=i*32;//位置
   Left:=10;
end;
end;


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多