Recent

Author Topic: does free pascal support control array's?  (Read 22019 times)

jw

  • Full Member
  • ***
  • Posts: 126
does free pascal support control array's?
« on: January 26, 2010, 11:54:56 pm »
Does free pascal support control array's like vb6 did?  How can I create and manage multiple controls easily without it?

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1931

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2645
Re: does free pascal support control array's?
« Reply #2 on: January 27, 2010, 12:35:15 am »
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)
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

Zaher

  • Hero Member
  • *****
  • Posts: 683
    • parmaja.org
Re: does free pascal support control array's?
« Reply #3 on: January 27, 2010, 11:53:37 am »
No.
You must create a list or dynamic array and add your controls manually to them (As Marc said).

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12195
  • FPC developer.
Re: does free pascal support control array's?
« Reply #4 on: January 27, 2010, 12:13:02 pm »
Can sb explain to me what control arrays, VB style actually are?

surendran

  • Newbie
  • Posts: 1
Re: does free pascal support control array's?
« Reply #5 on: January 27, 2010, 12:43:07 pm »
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

  • Administrator
  • Hero Member
  • *
  • Posts: 2645
Re: does free pascal support control array's?
« Reply #6 on: January 27, 2010, 04:08:10 pm »
Can sb explain to me what control arrays, VB style actually are?
If "we" put 10 buttons on a form you get something like
Code: Pascal  [Select][+][-]
  1.   TForm1=class(TForm)
  2.     Button1: TButton;
  3.     Button2 TButton;
  4.     ...
  5.     Button9: TButton;
  6.     Button10: TButton;
  7.  

in VB you can create them like in an array which results in something like:
Code: Pascal  [Select][+][-]
  1.   TForm1=class(TForm)
  2.     Button[1]: TButton;
  3.     Button[2] TButton;
  4.     ...
  5.     Button[9]: TButton;
  6.     Button[10]: TButton;
  7.  

So you can easily loop through them. Sometimes you need this in VB, but most times its a design flaw.
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

jw

  • Full Member
  • ***
  • Posts: 126
Re: does free pascal support control array's?
« Reply #7 on: February 01, 2010, 08:07:41 pm »
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.  

« Last Edit: February 01, 2010, 08:21:11 pm by jw »

Wodzu

  • Full Member
  • ***
  • Posts: 171
Re: does free pascal support control array's?
« Reply #8 on: February 01, 2010, 09:12:24 pm »
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 :)
« Last Edit: February 01, 2010, 09:14:43 pm by Wodzu »

jw

  • Full Member
  • ***
  • Posts: 126
Re: does free pascal support control array's?
« Reply #9 on: February 01, 2010, 09:49:25 pm »
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

  • Full Member
  • ***
  • Posts: 171
Re: does free pascal support control array's?
« Reply #10 on: February 01, 2010, 11:36:59 pm »
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

  • Hero Member
  • *****
  • Posts: 672
  • "Good sysadmin. Bad programmer."™
Re: does free pascal support control array's?
« Reply #11 on: March 17, 2010, 04:47:07 pm »
Thanks Wodzu, I needed this also ;)
Regards, ArNy the Turbo Rascal
-
"The secret is to give them what they need, not what they want." - Scotty, STTNG:Relics

JD

  • Hero Member
  • *****
  • Posts: 1905
Re: does free pascal support control array's?
« Reply #12 on: March 17, 2010, 05:02:00 pm »
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.  :D
Linux Mint - Lazarus 4.0/FPC 3.2.2,
Windows - Lazarus 4.0/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

Ask

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 687
Re: does free pascal support control array's?
« Reply #13 on: March 18, 2010, 05:21:52 am »
Quote
Thanks 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

  • Hero Member
  • *****
  • Posts: 672
  • "Good sysadmin. Bad programmer."™
Re: does free pascal support control array's?
« Reply #14 on: March 18, 2010, 04:07:59 pm »
We talked about control arrays in this thread: http://www.lazarus.freepascal.org/index.php/topic,8912.msg43323/topicseen.html#new

Zoran also attached a complete example project.
Regards, ArNy the Turbo Rascal
-
"The secret is to give them what they need, not what they want." - Scotty, STTNG:Relics

 

TinyPortal © 2005-2018