does free pascal support control array's? lazarus / 31-LCL ---------------------------------------------------------------------- jw · 2010-01-26 22:54 Does free pascal support control array's like vb6 did?  How can I create and manage multiple controls easily without it? ---------------------------------------------------------------------- theo · 2010-01-26 23:32 I don't know VB. Here are some ideas: http://www.freepascal.org/docs- html/rtl/classes/tcomponent.components.html http://www.freepascal.org/docs-html/fcl/contnrs/tcomponentlist.html ---------------------------------------------------------------------- Marc · 2010-01-26 23:35 and you can use a TList or a dyn array. It all depends on how you want to use them. (tbh, in most cases you don't need an array) ---------------------------------------------------------------------- Zaher · 2010-01-27 10:53 No. You must create a list or dynamic array and add your controls manually to them (As Marc said). ---------------------------------------------------------------------- marcov · 2010-01-27 11:13 Can sb explain to me what control arrays, VB style actually are? ---------------------------------------------------------------------- surendran · 2010-01-27 11:43 The following links also may be helpful (though related to Delphi). http://www.delphi3000.com/articles/article_3056.asp http://www.delphifaq.net/how-to-create-control-arrays/ I feel this is a natural question when a person first come from VB to Dellphi. Over the years I have heard this question many time from the first time learners who came from VB only. I feel many VB tutorials starts with a calculator program which uses control arrays. It can be done in Delphi/Lazurus/FP. But the question is when we are moving from one language to another, we need to think in the new languages way and methodology because each language has its own logic and 'philosophy'. I have read in a Pascal book that whenever a person move from VB he has to 'unlearn' a lot of things. And Object Pascal/FP is an OOP language and not just object based language. ---------------------------------------------------------------------- Marc · 2010-01-27 15:08 Quote from: marcov on January 27, 2010, 12:13:02 pmCan sb explain to me what control arrays, VB style actually are? If "we" put 10 buttons on a form you get something likeCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   TForm1=class(TForm)    Button1: TButton;    Button2 TButton;    ...  Button9: TButton;    Button10: TButton;  in VB you can create them like in an array which results in something like:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   TForm1=class(TForm)    Button[1]: TButton;    Button[2] TButton;    ...    Button[9]: TButton;    Button[10]: TButton;  So you can easily loop through them. Sometimes you need this in VB, but most times its a design flaw. ---------------------------------------------------------------------- jw · 2010-02-01 19:07 Ok here's how vb does control arrays and vb.net has done away with this because it's becoming oops like with microsofts little bugs.  I'm struggling with the concept in vb.net as well bacause we still create an array then add control objects to it, but all of a sudden it takes a lot more code.  It all seems academic, but I'm a practical type, I just want to make it work as fast as I can before I get bored with it and walk away. Ok sorry for the rant, all you need is one control object on the form a picturebox, button etc.  You do not need to create them at designtime just the first one which is as easy as cutting and pasting the control back to the form at design time.  VB then askes if you want to make it an array.   The array grows and shrinks dynamically so I can load create and destroy all at runtime. so to create 50 control array's of a picture box the code is for a=1 to 50     'the 0th control is created on design time so I actually have 51 load picturebox(a) next a you may think this is stupid but this is where it gets cool for like graphics for a=1 to int(rnd(50)*50 +1) load picturebox(a) next a now I can create timers to animate these picture's all over the screen.  The point being everytime the program is run it generates a different number of objects to be accessed as an array. Note I do not need to have the controls created on the form they are created and added to the array at runtime. It's looking like in pascal I must create an array and already have the objects on the form to add to the array then add the objects to the array at rutime.  This is similar to vb.net.  But what if I say the maximum amount of controls is 100 MyEdits : array [1..100] of TEdit; but useing my random creation of controls it chooses only to make 25 that means I've waisted 75% of an array I'm forced to define at design time. and I've got 75 tedit boxes sitting on the form tha't I'm not going to use so I need to make them invisible. However marc is saying I can make a dynamic array and add controls to it and I cannot believe that a control connot be generated at runtime so I'm still behind the learning curve. how does one create a dynamic array which you can add controls to? how do you generate controls at runtime to add to the array?  I don't want to create 100 controls at designtime to add to the array. I suppose anyone who started out learing object oriented programming in vb6 learned it wrong to start with and will have to relearn how to do it right.  When I took java it was a pain thing as well, but I've had the hardest time making the jump from DOS programming to windows.  One of my first question to my vb teacher was Where is the program running when nothing is happening the form is there but nothing is happening where the repeat until loop?  Well vb is event driven he says.  So I equated that to hooking bios or dos interrupts and staying resident in memory but it's kinda not. ---------------------------------------------------------------------- Wodzu · 2010-02-01 20:12 Hi jv. Why you can not belive that control can be created at runtime? :-) Graphic control is no different than any other object and as every object it can be created at runtime. The main difference between creating an object at runtime and designtime is that at runtime you must setup some (often a lot) properties which are easier to setup in designtime (or are set up automatically by IDE). Here is a code which will create a bunch of images at random position on the form. Code: [Select]var   Images: array of TImage; procedure TForm1.FormCreate(Sender: TObject); var   i: Integer;   MAX_IMAGES: Integer; begin   Randomize;   MAX_IMAGES := 25;   SetLength(Images, MAX_IMAGES);   for i := 0 to MAX_IMAGES - 1 do   begin     Images[i] := TImage.Create(Self);     TImage(Images[i]).Transparent := True;     TImage(Images[i]).Parent := Self;     TImage(Images[i]).Width := 32;     TImage(Images[i]).Height := 32;     TImage(Images[i]).Left := Random(Self.Width);     TImage(Images[i]).Top := Random(Self.Height);     TImage(Images[i]).Canvas.Brush.Color := clRed;     TImage(Images[i]).Canvas.Pen.Color := clNone;     TImage(Images[i]).Canvas.Ellipse(0,0,32,32);   end; end; I know that this is a longer piece of code compared to the VB but you may easly write a procedure wich will imitate the behaviour of VB code. In the above example, we create a dynamic array by setting its length. The length can be assigned dynamically over the time. To free an array set it to the nil Code: [Select]Images := nil Remeber that this only free the memory assigned for the array, it wont free your images. However, Images will be freed by their owner(just before the owner gets freed) which is in this example a form. Code which I've shown is just a one way of achiving this and probably not the best. You could keep your controls in TList or TObjectList also or derive a strong typed collection. However arrays are fast and small compared to the other solutions. In case of graphical controls you must be especialy aware of the their two properties which are Owner and Parent. Owner is responsible for freeing owned objects and parent is a control in which boundaries its children will be drawn. Hope this was somewhat helpful ---------------------------------------------------------------------- jw · 2010-02-01 20:49 thank you very much!!!! I'm on the irc trying to figure out what you've already posted got the TImage.Create(self); command but was haveing trouble compileing it because I didn't know how to define the handle which is right in your code. Images: array of TImage;  I believe i said " I cannot believe that a control connot be generated at runtime" bad grammer double negative,   which means it must be possible and I was doing my best at creating the exact code you posted thanks a lot!!! ---------------------------------------------------------------------- Wodzu · 2010-02-01 22:36 jw, no problem at all Here is a full listing of the code. The handle could be declared in the interface section, take a look: Code: [Select]unit Unit1; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,   ExtCtrls; type   { TForm1 }   TForm1 = class(TForm)     procedure FormCreate(Sender: TObject);     procedure Image1Click(Sender: TObject);   private     { private declarations }   public     { public declarations }   end; var   Form1: TForm1;   Images: array of TImage; implementation { TForm1 } procedure TForm1.FormCreate(Sender: TObject); var   i: Integer; MAX_IMAGES: Integer; begin   Randomize;   MAX_IMAGES := 25;   SetLength(Images, MAX_IMAGES); for i := 0 to MAX_IMAGES - 1 do   begin   Images[i] := TImage.Create(Self);     TImage(Images[i]).Transparent := True;   TImage(Images[i]).Parent := Self;   TImage(Images[i]).Width := 32;     TImage(Images[i]).Height := 32;     TImage(Images[i]).Left := Random(Self.Width);     TImage(Images[i]).Top := Random(Self.Height);     TImage(Images[i]).Canvas.Brush.Color := clRed;     TImage(Images[i]).Canvas.Pen.Color := clNone;     TImage(Images[i]).Canvas.Ellipse(0,0,32,32);                                            end; end; initialization   {$I unit1.lrs} end. I guess, if you create New->Application project under Lazarus, you can safely copy and paste this code and it will work. If you have ANY questions, don't hesitate to ask ---------------------------------------------------------------------- TurboRascal · 2010-03-17 15:47 Thanks Wodzu, I needed this also ---------------------------------------------------------------------- JD · 2010-03-17 16:02 Thanks Wodzu. I was planning to convert some Java code to Pascal to implement this feature for use in creating dynamic bar charts. I think I'll just go ahead & use your example. ---------------------------------------------------------------------- Ask · 2010-03-18 04:21 QuoteThanks Wodzu. I was planning to convert some Java code to Pascal to implement this feature for use in creating dynamic bar charts. Note that it is much easier to create bar charts using specialized components, like TChart (http://wiki.lazarus.freepascal.org/TAChart#About) ---------------------------------------------------------------------- TurboRascal · 2010-03-18 15:07 We talked about control arrays in this thread: http://www.lazarus.free pascal.org/index.php/topic,8912.msg43323/topicseen.html#new Zoran also attached a complete example project. ---------------------------------------------------------------------- jw · 2011-01-04 22:07 how to get mouse clicks for control arrays http://www.lazarus.freepascal.org/index.php/topic,10729.0.html