Re: Object array


[ Delphi Forum -- by DelphiLand ]

Posted by Feier Vlad on October 01, 2003 at 21:41:09:

In Reply to: Object array posted by Constantin on October 01, 2003 at 08:47:11:

: how to create component to have an array
: e.g. :
: Label1[0].Caption...
: Label2[1].Caption...
: .
--------------------

Well... your problem will be solved while the program is running. You must create components by writing, not by drawing them on form1. To create a single component you write this:

var 
  comp:TComponent;
  ...
begin
  ...
  comp := Tcomponent.Create(form);
  comp.parent := form;
  ...

where:
  comp - the variable's name of the component
  TComponent - the type of comp
  form - ( between '()' ) the 'parent' form, the form wich will contain the component

This was somehow the syntax of creating a component. An example here... (we create a label, after this we must set some properties to see it):

var 
  l:TLabel;
  ...
begin
  ...
  l:=TLabel.Create(form1);
  l.parent:=form1;    //set the parent of comp
    
  with l do begin
    top := 50; left := 50;
    width := 200; height := 20;
    caption := 'This is the new label';
    show;
  end; 
  ...
   

If you want to create an array of objects you must create them one by one:

var 
  l:array[1..10] of TLabel;
  ...
begin
  ...
  for i: = 1 to 10 do begin
    l[i] := TLabel.Create(form1);
    l[i].parent := form1;
    with l[i] do begin
      top := i*20; left := 50;
      height := 20; width := 200;
      caption := 'A new label';
      show;
    end;
  end;

Be sure you set the parent of your new component. You can set the parent another form (form2, ...) or a frame (frame21...).

Obs: Some components requires using of their special units (if you create a component TImage and load an image from a *.jpg file, you must write in the uses section 'uses ..., jpeg, ...')
You can also create matrices of components ...


Related Articles and Replies:


[ Delphi Forum ]
[ DelphiLand: free Delphi source code, tips, tutorials ]