Recent

Author Topic: Array of TForm problems  (Read 5466 times)

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Array of TForm problems
« on: July 24, 2016, 08:38:56 pm »
I need to have an unknown number of three different forms available at run time. I have created the three forms and set them as "Available Forms" named "frmOne", "frmTwo" and "frmThree" There could be five of frmOne, seven of frmTwo etc.

I have defined a record
Code: [Select]
type
  TFrmRec = record
    ID : Integer;
    Frm : TForm;
  end;
var
     FrmArr : Array of TFrmRec;

begin

That compiles OK, but, how do I assign and use the forms as needed at run time? So far I have...

Code: [Select]
var
  i : Integer;
  Indx : Integer;
begin
  Indx:=High(FrmArr)+1;
  FrmArr[Indx].ID:=9;  // Or whatever is passed to here
  FrmArr[Indx].Frm...

But not sure where  to go from here and how to "Show" the Forms. 
[EDIT]
(Ooops, that was "ShowModal" but I need "Show" as there can be several versions of the one form visible but with their own data.
[/EDIT]

I did some searching but can't find anything with Forms in an Array.
« Last Edit: July 24, 2016, 11:54:09 pm by HatForCat »
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

bylaardt

  • Sr. Member
  • ****
  • Posts: 309
Re: Array of TForm problems
« Reply #1 on: July 24, 2016, 09:52:37 pm »
In this case, TList instead array is a best choice.
If you use ID, you can define a Integer in the form.


Code: Pascal  [Select][+][-]
  1. For i:=1 to 5 do
  2.   MyList1.add(TFrmOne.create(application));
  3.  
  4. For i:=1 to 7 do
  5.   MyList2.add(TFrmTwo.create(application));
  6.  
  7. For i:=1 to 7 do
  8.   MyList3.add(TFrmtree.create(application));
  9.  
  10. //and showmodal:
  11.  
  12. TFrmOne(MyList1[MyChosenIndex]).showmodal;
  13.  

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Array of TForm problems
« Reply #2 on: July 24, 2016, 09:53:34 pm »
The attached project shows one way to accomplish this.

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: Array of TForm problems
« Reply #3 on: July 24, 2016, 10:01:54 pm »
In this case, TList instead array is a best choice.

Thanks, I'll give that a try.
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: Array of TForm problems
« Reply #4 on: July 24, 2016, 10:21:51 pm »
It might be of interest that there is already a TFormList held automatically.
Screen.Forms (It's declared as FFormList: TFPList; in Screen)

So why not just access Screen.Forms instead of keeping a list for yourself. You could even use the tag-property of the form as form of identification if you want (or use caption or something).

Code: Pascal  [Select][+][-]
  1. var
  2.   I: Integer;
  3. begin
  4.   // you can create your forms but not show them
  5.   // they will still be in Screen.Forms list
  6.   for I := 0 to Screen.FormCount - 1 do
  7.   begin
  8.     if Screen.Forms[I].Tag = 1001 then
  9.     begin
  10.       Screen.Forms[I].ShowModal;
  11.       // or do something else with the form
  12.       if Screen.Forms[I] is TForm1 then
  13.         TForm1(Screen.Forms[I]).Button1.Click;
  14.     end;
  15.   end;
  16. end;
« Last Edit: July 24, 2016, 10:27:31 pm by rvk »

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: Array of TForm problems
« Reply #5 on: July 24, 2016, 11:51:46 pm »
Screen.Forms (It's declared as FFormList: TFPList; in Screen)

Thanks, I just had a play with that but there is too much going on within some of the forms and I need them to retain their own data so I can BringToFront if they are already showing.
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: Array of TForm problems
« Reply #6 on: July 25, 2016, 12:09:27 am »
... but there is too much going on within some of the forms and I need them to retain their own data so I can BringToFront if they are already showing.
The forms do retain all their data. It's just a pointer to ANY form inherited from TForm (which all forms are).

You can still access that data:
Code: Pascal  [Select][+][-]
  1. if Screen.Forms[I] is TMySpecialForm then
  2.   TMySpecialForm(Screen.Forms[I]).MyExtraSpeciaData := 'yeah baby';
  3. if Screen.Forms[I] is TMyOtherSpecialForm then
  4.   TMyOtherSpecialForm(Screen.Forms[I]).Show;
  5. // you can do anything with casting the correct form

You can create your own TFormList if you want but all the forms are always already in Screen.Forms :)

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: Array of TForm problems
« Reply #7 on: July 26, 2016, 06:55:34 pm »
You can still access that data:

Thanks, I should have left "data" out of that recent statement, on one form there are nine Up-Down controls, seven buttons, two DBGrids, three pages in a TabControl and more. All forms have similar things going on. Just too much to handle by proxy. :)

But, thanks for trying to help.

Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: Array of TForm problems
« Reply #8 on: July 26, 2016, 07:04:34 pm »
... on one form there are nine Up-Down controls, seven buttons, two DBGrids, three pages in a TabControl and more. All forms have similar things going on. Just too much to handle by proxy. :)
Mmmm, I'm still not sure how you are going to handle this then with a TList (or something else). You'll run into the same "problem" as with using just Screen.Forms.

But ok, I'll take it you know what you're doing in your own project.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Array of TForm problems
« Reply #9 on: July 26, 2016, 07:56:32 pm »
Yes. But - given the dit - I also wonder if he realizes that every version of the same form needs to be instantiated = created first.

The answers from Bylaart and Rik should be sufficient if he knows that already.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: Array of TForm problems
« Reply #10 on: July 28, 2016, 05:40:32 pm »
Yes. But - given the dit -

What is a "dit?"

I also wonder if he realizes that every version of the same form needs to be instantiated = created first.

Yes, he does! :)


Perhaps you missed this salient bit of my OP, "an unknown number"

There could be 100 of form1 and none of form2 or form3, or 10 of form1, 7 of form2 or any numbers the the user decides ho many of each they need to watch.

It is always more helpful on a "help forum," to actually offer help rather than denigrate the poster. If you know how it should be done, then please help, by posting example code.
http://www.oxforddictionaries.com/us/definition/american_english/help


Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: Array of TForm problems
« Reply #11 on: July 28, 2016, 07:49:55 pm »
The attached project shows one way to accomplish this.
the attached sample by howardpc works fine,,
did you try it? what's wrong with it? what's missing?

 

TinyPortal © 2005-2018