faqts : Computers : Programming : Languages : Delphi

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

5 of 7 people (71%) answered Yes
Recently 5 of 7 people (71%) answered Yes

Entry

Delphi: Component: Dynamic: Create: How to create button on focus tabsheet, when clicking button?

Sep 19th, 2004 12:48
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 12 December 2003 - 01:47 am -------------------

Delphi: Component: Dynamic: Create: How to create button on focus 
tabsheet, when clicking button?

---

Steps: Overview:

 1. -Start a new Delphi application

 2. -Put several tabsheets on a form

 3. -Put a button below this tabsheets

 4. -Double click on the button

 5. -Replace the source code with
     the following text:

---
---

--- cut here: begin --------------------------------------------------

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    TabSheet3: TTabSheet;
    TabSheet4: TTabSheet;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }

  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  buttonx : array [0..100] of integer;
  buttonymin : array [0..100] of integer;
  buttony : array [0..100] of integer;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var Btn : TButton;
var componentT : TComponent;
var I : integer;
 begin
  PageControl1.Visible := true;
  I := PageControl1.ActivePageIndex;
  Btn := TButton.Create(Self);
  Btn.Left := buttonx[ I ];
  Btn.Top := buttony[ I ];
  buttony[ I ] := buttony[ I ] + Btn.Height;
  if buttony[ I ] >= 300 then begin
   buttonx[ I ] := buttonx[ I ] + Btn.Width;
   buttony[ I ] := buttonymin[ I ];
  end;
  componentT := FindComponent( 'Tabsheet' + IntToStr( 
PageControl1.ActivePageIndex + 1 ) ) as TTabsheet;
  if Assigned( componentT ) then begin
   with componentT as TTabSheet do begin
    Btn.Parent := TWinControl( componentT );
   end;
  end;
end;

end.

--- cut here: end ----------------------------------------------------

 6. If you run this program, each time you click the button
    if will dynamically create a button on the current
    tabsheet (row after row, column after column)

---
---

Hereby the unit1.dfm file:

--- cut here: begin --------------------------------------------------

object Form1: TForm1
  Left = 192
  Top = 127
  Width = 870
  Height = 500
  Caption = 'Form1'
  Color = clInfoBk
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object PageControl1: TPageControl
    Left = 0
    Top = 0
    Width = 862
    Height = 417
    ActivePage = TabSheet4
    Align = alTop
    TabOrder = 0
    object TabSheet1: TTabSheet
      Caption = 'TabSheet1'
    end
    object TabSheet2: TTabSheet
      Caption = 'TabSheet2'
      ImageIndex = 1
    end
    object TabSheet3: TTabSheet
      Caption = 'TabSheet3'
      ImageIndex = 2
    end
    object TabSheet4: TTabSheet
      Caption = 'TabSheet4'
      ImageIndex = 3
    end
  end
  object Button1: TButton
    Left = 360
    Top = 424
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
end

--- cut here: end ----------------------------------------------------

---
---

Internet: see also:

---

Delphi: Can you give an overview of links of how to dynamically create 
new components?
http://www.faqts.com/knowledge_base/view.phtml/aid/31466/fid/175

----------------------------------------------------------------------