Recent

Author Topic: [SOLVED] \o/ Set-Define / Get-Retrive array name  (Read 7732 times)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Set-Define / Get-Retrive array name
« Reply #15 on: December 19, 2017, 07:57:01 pm »
To be able to refer to instantiated controls (buttons or whatever) in code you can use at least  these means:
  • refer to the controls by reference (storing the references as pointers in an array or list at creation)
  • refer to the controls by Name (you can store the Names in an array or list or some other container, or you can rely on the LCL's internal Controls or Components array - but this string-based lookup is slow for large numbers of controls; and you have to take care that each name is unique if you create and name the controls dynamically)
  • refer to the controls by Tag. This is just a variation on using Name, but quicker to index/locate, and the same caveat applies to make each Tag unique
  • add some fresh property or field to the control as an ID, and ignore the above built-in means
Of course you can combine any of these methods, as well.

douglas.cast

  • Guest
Re: Set-Define / Get-Retrive array name
« Reply #16 on: December 19, 2017, 08:30:07 pm »
To be able to refer to instantiated controls (buttons or whatever) in code you can use at least  these means:
  • refer to the controls by reference (storing the references as pointers in an array or list at creation)
  • refer to the controls by Name (you can store the Names in an array or list or some other container, or you can rely on the LCL's internal Controls or Components array - but this string-based lookup is slow for large numbers of controls; and you have to take care that each name is unique if you create and name the controls dynamically)
  • refer to the controls by Tag. This is just a variation on using Name, but quicker to index/locate, and the same caveat applies to make each Tag unique
  • add some fresh property or field to the control as an ID, and ignore the above built-in means
Of course you can combine any of these methods, as well.

Give headaches to all guys in the forum can be considered a new development style? LOL

Just laughing out here, you guys really bless my days.

In the end I can't find what I was looking for in the begging, but thanks to all the replies and ideas and codes, I figure out a way to do what I want, was far away from my initial thinking, but fits like a glove, works like a charm or any other sweet expression that can be used like a "thanks".

I mixed up almost all replies and get this code, the exactly (result) of what I want, need, etc.

Once more thanks very much for all support and patience with my lack of knowledge in english.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls;
  9.  
  10. type
  11.   pArray          = Array of TPanel;
  12.   bArray          = Array[0..2] of Array[1..6] of TButton;
  13.   { TButtonPanel }
  14.  
  15.   { TForm1 }
  16.  
  17.   TForm1 = class(TForm)
  18.     Button1: TButton;
  19.     p1: TPanel;
  20.     p2: TPanel;
  21.     p3: TPanel;
  22.     p4: TPanel;
  23.     procedure FormCreate(Sender: TObject);
  24.   private
  25.     { private declarations }
  26.     procedure CreateButtons;
  27.     procedure myButtonClick(Sender:TObject);
  28.     procedure theClick(btn:TButton);
  29.   public
  30.     { public declarations }
  31.     b:bArray;
  32.   end;
  33.  
  34. var
  35.   Form1: TForm1;
  36.  
  37. implementation
  38.  
  39. {$R *.lfm}
  40.  
  41. { TForm1 }
  42.  
  43. procedure TForm1.FormCreate(Sender: TObject);
  44. begin
  45.   CreateButtons;
  46. end;
  47.  
  48. procedure TForm1.CreateButtons;
  49. var
  50.   i, h, c:Integer;
  51.   pk:TPanel;
  52.   panelArray : pArray;
  53. begin
  54.   panelArray:=panelArray.Create(p1, p2, p3);
  55.   c := Low(panelArray);
  56.     for pk in panelArray do begin
  57.       h := panelArray[c].Top;
  58.       //
  59.       for i:=Low(b[0]) to High(b[0]) do begin
  60.         b[c][i]:=TButton.Create(Self);
  61.         with b[c][i] do begin
  62.           Name:=panelArray[c].Name+'_'+IntToStr(i);
  63.           Caption:=Name;
  64.           Parent:=panelArray[c];
  65.           Tag:=c;
  66.           Top:=h;
  67.           Width:=panelArray[c].Width;
  68.           //
  69.           OnClick:=@myButtonClick;
  70.         end;
  71.         h:=h+30;
  72.       end;
  73.       c:=c+1;
  74.       //
  75.     end;
  76. end;
  77.  
  78. procedure TForm1.myButtonClick(Sender: TObject);
  79. var
  80.   bc:TButton;
  81. begin
  82.   bc := Sender as TButton;
  83.   theClick(bc);
  84. end;
  85.  
  86. procedure TForm1.theClick(btn: TButton);
  87. var
  88.   i:Integer;
  89. begin
  90.   p4.Caption:='Nome: '+btn.Name+' TAG: '+IntToStr(btn.Tag);
  91.   for i:=Low(b[0]) to High(b[0]) do begin
  92.     b[btn.Tag][i].Font.Style:=[];
  93.   end;
  94.     btn.Font.Style:=[fsBold, fsItalic];
  95. end;
  96.  
  97. end.
  98.  

How can I mark like: SOLVED?
« Last Edit: December 19, 2017, 08:34:56 pm by douglas.cast »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Set-Define / Get-Retrive array name
« Reply #17 on: December 19, 2017, 08:50:02 pm »
How can I mark like: SOLVED?

As originator of the thread you (alone) can edit the subject.

 

TinyPortal © 2005-2018