Board index » delphi » Creating Controls at Runtime

Creating Controls at Runtime

Hello,

I need to be create Edit fields on a form, which the user will be able to
specify how many. I'm guessing the most efficient way of doing this is
creating the controls at runtime. I've looked at some examples, however I've
noticed that creating buttons or edit fields requires the object to be
declared...

I need to be able to create TEdit controls infinitely. Any suggestions on
how I can create a TEdit field at runtime and have a unique name for each
Edit field. I would need to create these fields in a  FOR loop..

Any suggestions?

Thanks in advance.

Trevor.

 

Re:Creating Controls at Runtime


Here is a slightly over the top demo of creating arrays of Labels and
Edit boxes - you will note that the Name is an optional property.

You could build a 'name' like 'MyEdit[' + IntToStr(L9) + ']'

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    Procedure SetupLabels(Var Lbl: Array Of TLabel );
    Procedure SetupBoxes(Var EditBox: Array Of TEdit; L:Integer );
    Procedure EditboxChangeEvent( Sender:TObject );
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
Var
LabelDaysUsed : Array Of TLabel;
EditBoxAdd : Array Of TEdit;
EditBoxSub : Array Of TEdit;
MaxEdit : Integer = 8 ;

// Here we create Arrays Labels & Edit Boxes
procedure TForm1.FormCreate(Sender: TObject);
Begin
  SetLength( LabelDaysUsed, MaxEdit );
  SetUpLabels( LabelDaysUsed );
  //
  SetLength( EditBoxAdd, MaxEdit );
  SetupBoxes( EditBoxAdd, 50 );
  //
  SetLength( EditBoxSub, MaxEdit );
  SetupBoxes( EditBoxSub, 130 );
End;

// Setup an Array of Labels
Procedure TForm1.SetupLabels(Var Lbl: Array Of TLabel );
Var
  L9: Integer;
  LB: TLabel;
begin

  For L9 := 0 To High(Lbl) Do
      Begin
        Lbl[L9] := TLabel.Create( Self );
        LB := Lbl[L9];
        LB.Left := 0;
        LB.Height := 30;
        LB.Width := 50;
        LB.Top := L9 * (LB.Height + 3);
        LB.Parent := Self;
        LB.Visible := True;
        LB.Caption := IntToStr( L9 ) ;
      End;
End;

// Setup An Array of EditBoxes
Procedure TForm1.SetupBoxes(Var EditBox: Array Of TEdit; L:Integer );
Var
  L9: Integer;
  EB: TEdit;
begin

  For L9 := 0 To High(EditBox) Do
      Begin
        EditBox[L9] := TEdit.Create( Self );
        EB := EditBox[L9];
        EB.Left := L;
        EB.Height := 30;
        EB.Width := 50;
        EB.Top := L9 * (EB.Height + 3);
        EB.Parent := Self;
        EB.Visible := True;
        EB.OnChange := EditboxChangeEvent;
        EB.Tag := L9 ;
      End;
End;

// Return a number from a dirty String
Function StrValInt( Const S:String ):Integer;
Var
  ErrPos: Integer;
Begin
  Val( S, Result, ErrPos );
End;

// The On Change Event
Var
InRoutineFlag: Boolean = False;
procedure TForm1.EditboxChangeEvent(Sender: TObject);
Var
  Current:Integer;
  Q, DaysDone: Integer;
  S: String;
begin
  // Recursion can be {*word*193}
  If InRoutineFlag Then Exit;

  // Check we have not done some sloppy coding
  If (TObject(Sender) Is TEdit) = False Then
     Begin
       ShowMessage( 'Bad Sender' ) ;
       Exit;
     End;
  //
  InRoutineFlag := True;

  // So we know the Number of the Line
  Current := TEdit(Sender).Tag;
  //
  S := LabelDaysUsed[Current].Caption;
  DaysDone := StrValInt( S );

  Q := DaysDone + StrValInt( EditBoxAdd[Current].Text )
                - StrValInt( EditBoxSub[Current].Text ) ;
  If Q < 0 Then
     Begin
       ShowMessage( 'Do not get cute !' );
       TEdit(Sender).Text := '0' ;
       TEdit(Sender).SetFocus ;
     End;

  InRoutineFlag := False;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
     EditBoxAdd[2].Name := 'Fred' ;
     ShowMessage( EditBoxAdd[2].Name ) ;
end;

end.

On Fri, 23 Nov 2001 09:34:21 -0500, "Trevor McIntyre"

Quote
<ad...@netrubric.ca> wrote:
>Hello,

>I need to be create Edit fields on a form, which the user will be able to
>specify how many. I'm guessing the most efficient way of doing this is
>creating the controls at runtime. I've looked at some examples, however I've
>noticed that creating buttons or edit fields requires the object to be
>declared...

>I need to be able to create TEdit controls infinitely. Any suggestions on
>how I can create a TEdit field at runtime and have a unique name for each
>Edit field. I would need to create these fields in a  FOR loop..

>Any suggestions?

>Thanks in advance.

>Trevor.

Re:Creating Controls at Runtime


Trevor McIntyre <ad...@netrubric.ca> schreef in berichtnieuws
L7tL7.22649$op.3314...@news20.bellglobal.com...

Quote
> Hello,

> I need to be create Edit fields on a form, which the user will be able to
> specify how many. I'm guessing the most efficient way of doing this is
> creating the controls at runtime. I've looked at some examples, however
I've
> noticed that creating buttons or edit fields requires the object to be
> declared...

> I need to be able to create TEdit controls infinitely. Any suggestions on
> how I can create a TEdit field at runtime and have a unique name for each
> Edit field. I would need to create these fields in a  FOR loop..

> Any suggestions?

> Thanks in advance.

> Trevor.

use RegisterClass(TEdit) to register the components......

Re:Creating Controls at Runtime


Quote
"J French" <je...@iss.u-net.com> wrote in message

news:3bfe663b.16579840@news.u-

Quote
> implementation

> {$R *.DFM}
> Var
> LabelDaysUsed : Array Of TLabel;
> EditBoxAdd : Array Of TEdit;
> EditBoxSub : Array Of TEdit;
> MaxEdit : Integer = 8 ;

Its not a good idea to declare these arrays as unit global items. They
should be declared as private or public fields of the tForm1 class. As
declared all instances of tForm1 would reference the same variables,
effectively eliminating the possibility of having two tForm1 active in the
same program.

Re:Creating Controls at Runtime


On Fri, 23 Nov 2001 13:49:03 -0500, "Bruce Roberts"

Quote
<b...@bounceitattcanada.xnet> wrote:

>"J French" <je...@iss.u-net.com> wrote in message
>news:3bfe663b.16579840@news.u-

<snip>
>Its not a good idea to declare these arrays as unit global items. They
>should be declared as private or public fields of the tForm1 class. As
>declared all instances of tForm1 would reference the same variables,
>effectively eliminating the possibility of having two tForm1 active in the
>same program.

Understood and agreed - it was just a test I ran up some time ago to
demonstrate 'creating' controls on the fly

But I stand corrected - one should not post 'dangerous' code.

Quote

Other Threads