Recent

Author Topic: how to address many same classes in 1 Tform  (Read 2873 times)

Haeleng

  • New Member
  • *
  • Posts: 10
how to address many same classes in 1 Tform
« on: May 29, 2017, 10:59:52 am »
Hi,
I am new to Lazarus and OOPS programming. In the 80's I worked with Pascal and then went to another job.
Now I want to pick up Pascal for a hobby.
I want to use the graphical interface of Lazarus with the object inspector.
I already made a program creating Listboxes in my source code, but than I get lost with getting it graphically alright, because I also use TpageControl.
So now I have a TForm with many Tlistboxes and graphically it is ok.
  TForm1 = class(TForm)   
    ListbChan1: TListBox;
    ListbChan2: TListBox;   
    .
    .
   ListbChan40: Tlistbox;
  end;

I have an input array that has to be written into the TlistBox items
I can address each of the 40 ListbChan seperately.
  ListbChan1.Items[0] := InputArray.chan1.String[0];
  ListbChan1.Items[1] := InputArray.chan1.String[1];
  ListbChan2.Items[0] := InputArray.chan2.String[0];
  ListbChan2.Items[1] := InputArray.chan2.String[1];
  .
  .
  ListbChan40.Items[0] := InputArray.chan40.String[0;]
  ListbChan40.Items[1] := InputArray.chan40.String[1];
So I will have very much lines of code because I have even more items for the Listbox
Normally I should make an array of Tlistbox types and put it in Tform, but this is not allowed (error message)
Then fill the items of this listbox array with a for loop
How can I best address this issue. Please give a example with code.
Thanks in advance

sky_khan

  • Guest
Re: how to address many same classes in 1 Tform
« Reply #1 on: May 29, 2017, 11:21:29 am »
You can create and fill components in code;

Code: Pascal  [Select][+][-]
  1. type
  2.   TForm1 = class(TForm)
  3.     procedure FormCreate(Sender: TObject);
  4.   private
  5.     { private declarations }
  6.     FBoxes : array of TListBox;
  7.  
  8. ...
  9.  
  10. procedure TForm1.FormCreate(Sender: TObject);
  11. var
  12.   i : integer;
  13. begin
  14.   SetLength(FBoxes,20);
  15.   for i:=0 to 19 do
  16.   begin
  17.     FBoxes[i]:=TListBox.Create(self);
  18.     FBoxes[i].Left:=(i mod 5)*105+5;
  19.     FBoxes[i].Top:=(i div 5)*85+5;
  20.     FBoxes[i].Items.Add(Format('%2d,%2d',[i div 5,i mod 5]));
  21.     FBoxes[i].Parent:=Self;
  22.   end;
  23. end;
  24.  

Haeleng

  • New Member
  • *
  • Posts: 10
Re: how to address many same classes in 1 Tform
« Reply #2 on: May 29, 2017, 11:43:57 am »
Hi SkyKhan,

I already filled components (Listboxes) in code.
But then I ran into the problem with TPageControl.
In TPageControl I have 3 tabs (Input, Output, Testmode).
I like to put the listboxes on the input tab, but when I switch between tabs, I see them on all 3 tabs.
How do I get the filled Listboxes on the correct tab of TPageControl. In this case Input and not on the Output and Testmode tab.
Thanks.
« Last Edit: May 29, 2017, 11:53:14 am by Haeleng »

sky_khan

  • Guest
Re: how to address many same classes in 1 Tform
« Reply #3 on: May 29, 2017, 12:00:09 pm »
Parent property determines where they will be shown. So if you want them on tabsheets you should set their parent property accordingly.
e.g
Code: Pascal  [Select][+][-]
  1. FBoxes[i].Parent:=tsInput;  // I assume you named your tabsheets (page control's pages) as tsInput, tsOutput, tsTestMode.

Haeleng

  • New Member
  • *
  • Posts: 10
Re: how to address many same classes in 1 Tform
« Reply #4 on: May 29, 2017, 12:44:34 pm »
Hi SkyKhan

I get an error Raised exception class External: SIGSEGV in Unit1.pas at the place where I put your code FBoxes.Parent:= tsInput;
For some reason the Bracket i Bracket does is not shown up after saving.
I use different names but it should not matter. Principle is the same.
What am I doing wrong?
« Last Edit: May 29, 2017, 12:55:39 pm by Haeleng »

sky_khan

  • Guest
Re: how to address many same classes in 1 Tform
« Reply #5 on: May 29, 2017, 02:13:52 pm »
I'm not good at English. So here is an example project
See attachment

Haeleng

  • New Member
  • *
  • Posts: 10
Re: how to address many same classes in 1 Tform
« Reply #6 on: May 29, 2017, 05:47:12 pm »
Hi SkyKhan,

Thank you very much. I never never should have solved this error

 

TinyPortal © 2005-2018