Recent

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

douglas.cast

  • Guest
[SOLVED] \o/ Set-Define / Get-Retrive array name
« on: December 04, 2017, 03:34:18 pm »
Hi all.

Inside of my code I need to get / "retrieve" the name of a array to play with their elements, how can I get / "retrieve" the array name.

Code: Pascal  [Select][+][-]
  1. type
  2. arrayOfButtons = Array of TButton;
  3.  
  4. //Elsewhere in the code
  5. mainArray : arrayOfButtons;
  6.  
  7. procedure TfrmMain.font_Style(btnName:TButton; arrayName:arrayOfButtons);
  8. var
  9.   c:Integer;
  10. begin
  11.   for c:= 0 to 4 do begin
  12.     with arrayName[c] do begin
  13.   //with "mainArray[c]" do begin
  14.       Font.Style:=[];
  15.     end;
  16.   end;
  17.   with btnName do begin
  18.     Font.Style:=[fsBold];
  19.     Font.Style:=[fsItalic];
  20.   end;
  21. end;
  22.  

I need to know how can I "pass" the array name to the function (and if I'm using the right definition in the procedure [array of buttons] or if I should use something else [array name, string, etc]).
« Last Edit: December 20, 2017, 01:49:26 am by douglas.cast »

KemBill

  • Jr. Member
  • **
  • Posts: 74
Re: Set-Define / Get-Retrive array name
« Reply #1 on: December 04, 2017, 03:46:06 pm »
you can loop between your form's buttons instead

Code: Pascal  [Select][+][-]
  1.   for i := 0 to form1.ComponentCount-1 do
  2.   begin
  3.     if form1.Components[i] is tbutton then
  4.     begin
  5.       tbutton(form1.Components[i]).Caption:= inttostr(i);
  6.       //do something else
  7.     end;
  8.   end;
  9.  

I would have used a tlist instead of arrays allowing to do

Code: Pascal  [Select][+][-]
  1. if (list.indexof(button) <> -1) then
  2. begin
  3.   //the button is in the list
  4. end;
  5.  
« Last Edit: December 04, 2017, 03:49:22 pm by KemBill »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Set-Define / Get-Retrive array name
« Reply #2 on: December 04, 2017, 04:54:19 pm »
You could try this. In a new Lazarus project double-click on the empty form to generate an OnCreate handler, and complete as follows:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   SysUtils, Forms, Controls, Graphics, StdCtrls, Spin;
  9.  
  10. type
  11.  
  12.   TArrayofButtons = array of TButton;
  13.  
  14.   TForm1 = class(TForm)
  15.     procedure FormCreate(Sender: TObject);
  16.   private
  17.     FButtonIndexSpinEdit: TSpinEdit;
  18.     FMainButtonArray: TArrayofButtons;
  19.     procedure FButtonIndexSpinEditChange(Sender: TObject);
  20.     procedure SetButtonFontStyle(aButtonIndex: Integer; aButtonArray: TArrayofButtons);
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. procedure TForm1.FormCreate(Sender: TObject);
  31. const
  32.   ButtonArrayLength = 5; // change to suit
  33. var
  34.   i: Integer;
  35. begin
  36.   SetLength(FMainButtonArray, ButtonArrayLength);
  37.   for i:=0 to High(FMainButtonArray) do begin
  38.     FMainButtonArray[i]:=TButton.Create(Self);
  39.     with FMainButtonArray[i] do begin
  40.       Caption:=Format('Button#%d',[i]);
  41.       Left:=10;
  42.       Top:=10 + i*30;
  43.       Parent:=Self;
  44.     end;
  45.   end;
  46.   FButtonIndexSpinEdit:=TSpinEdit.Create(Self);
  47.   with FButtonIndexSpinEdit do begin
  48.     MaxValue:=ButtonArrayLength - 1;
  49.     Left:=150;
  50.     Top:=10;
  51.     OnChange:=@FButtonIndexSpinEditChange;
  52.     Parent:=Self;
  53.   end;
  54. end;
  55.  
  56. procedure TForm1.FButtonIndexSpinEditChange(Sender: TObject);
  57. begin
  58.   SetButtonFontStyle(FButtonIndexSpinEdit.Value, FMainButtonArray);
  59. end;
  60.  
  61. procedure TForm1.SetButtonFontStyle(aButtonIndex: Integer; aButtonArray: TArrayofButtons);
  62. // note you don't need the second parameter, since you can refer to FMainButtonArray directly, but I left it in
  63. // the code since your program may need it.
  64. var
  65.   b: TButton;
  66. begin
  67.   // clear the Font.Style of all buttons in the array
  68.   for b in aButtonArray do
  69.     b.Font.Style:=[];
  70.  
  71.   // set Font.Style of the indexed button
  72.   if (aButtonIndex >= 0) and (aButtonIndex < Length(aButtonArray)) then
  73.     aButtonArray[aButtonIndex].Font.Style:=
  74.       aButtonArray[aButtonIndex].Font.Style + [fsBold, fsItalic];
  75. end;
  76.  
  77. end.

douglas.cast

  • Guest
Re: Set-Define / Get-Retrive array name
« Reply #3 on: December 05, 2017, 05:36:33 pm »
you can loop between your form's buttons instead

Not sure, probably a bad ideia to my application, in the end I'll have something like 50 to 100 buttons on the form, and I need to set this code to a dinamically created list of buttons of 0 to N.

So, if I have 50 buttons and make 5 searches on a loop this will take a lot of time, if lazarus / pascal "search engine" works with a "jump" to name of the element, like:

A button called zeroGravity create a jump to the buttons that start with the "z" letter, it wont matter that much.

If this "search engine" compare each name of each button like:

astronautName.Name = zeroGravity.Name...  this will take a lot, considering that my buttons witll start with M, P and V lettters.

I would have used a tlist instead of arrays allowing to do

Something like: (?)

Code: Pascal  [Select][+][-]
  1. type
  2.    myButtonList = TList of TButtons;
  3.  

You could try this. In a new Lazarus project double-click on the empty form to generate an OnCreate handler, and complete as follows:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   SysUtils, Forms, Controls, Graphics, StdCtrls, Spin;
  9.  
  10. type
  11.  
  12.   TArrayofButtons = array of TButton;
  13.  
  14.   TForm1 = class(TForm)
  15.     procedure FormCreate(Sender: TObject);
  16.   private
  17.     FButtonIndexSpinEdit: TSpinEdit;
  18.     FMainButtonArray: TArrayofButtons;
  19.     procedure FButtonIndexSpinEditChange(Sender: TObject);
  20.     procedure SetButtonFontStyle(aButtonIndex: Integer; aButtonArray: TArrayofButtons);
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. procedure TForm1.FormCreate(Sender: TObject);
  31. const
  32.   ButtonArrayLength = 5; // change to suit
  33. var
  34.   i: Integer;
  35. begin
  36.   SetLength(FMainButtonArray, ButtonArrayLength);
  37.   for i:=0 to High(FMainButtonArray) do begin
  38.     FMainButtonArray[i]:=TButton.Create(Self);
  39.     with FMainButtonArray[i] do begin
  40.       Caption:=Format('Button#%d',[i]);
  41.       Left:=10;
  42.       Top:=10 + i*30;
  43.       Parent:=Self;
  44.     end;
  45.   end;
  46.   FButtonIndexSpinEdit:=TSpinEdit.Create(Self);
  47.   with FButtonIndexSpinEdit do begin
  48.     MaxValue:=ButtonArrayLength - 1;
  49.     Left:=150;
  50.     Top:=10;
  51.     OnChange:=@FButtonIndexSpinEditChange;
  52.     Parent:=Self;
  53.   end;
  54. end;
  55.  
  56. procedure TForm1.FButtonIndexSpinEditChange(Sender: TObject);
  57. begin
  58.   SetButtonFontStyle(FButtonIndexSpinEdit.Value, FMainButtonArray);
  59. end;
  60.  
  61. procedure TForm1.SetButtonFontStyle(aButtonIndex: Integer; aButtonArray: TArrayofButtons);
  62. // note you don't need the second parameter, since you can refer to FMainButtonArray directly, but I left it in
  63. // the code since your program may need it.
  64. var
  65.   b: TButton;
  66. begin
  67.   // clear the Font.Style of all buttons in the array
  68.   for b in aButtonArray do
  69.     b.Font.Style:=[];
  70.  
  71.   // set Font.Style of the indexed button
  72.   if (aButtonIndex >= 0) and (aButtonIndex < Length(aButtonArray)) then
  73.     aButtonArray[aButtonIndex].Font.Style:=
  74.       aButtonArray[aButtonIndex].Font.Style + [fsBold, fsItalic];
  75. end;
  76.  
  77. end.

It's a wonderful code and it helped me (for different reasons) to clear a little bit my code:

Simple and beaultiful.

Code: Pascal  [Select][+][-]
  1.   for b in aButtonArray do
  2.     b.Font.Style:=[];
  3.  

Both codes are great, but It doesn't help me like I need, I want to create a procedure that change individual arrays or tlists, that's why I'm trying to "get / set" a arrays name, It's a weird idea (and explanation), but I need to treat the array or list like a compoment.

Both codes are doing something like this:

Code: Pascal  [Select][+][-]
  1.  
  2. procedure tForm1.arrayM (b: tButton):
  3. begin
  4.    //Code to mess up with elements in the arrayM
  5. end;
  6.  
  7. procedure tForm1.arrayV (b: tButton):
  8. begin
  9.    //Code to mess up with elements in the arrayV
  10. end;
  11.  
  12. procedure tForm1.arrayP (b: tButton):
  13. begin
  14.    //Code to mess up with elements in the arrayP
  15. end;
  16.  

What I'm trying to do is one procedure or function that works like this:

Code: Pascal  [Select][+][-]
  1. procedure tForm1.anyArray (b: tButton;  globalArray: Array of buttons {just some way to get the array or list name}):
  2. begin
  3.    //Code to mess up with elements in the globalArray { M, V or P that I'll get from the array }
  4. end;
  5.  

I'm wondering how to share this code between 3 different arrays (lists) and call it inside a button onclick procedure.

It doesn't matter if I need to create a bridge from globalArray to a localArray and them return this value to globalArray.

Code: Pascal  [Select][+][-]
  1. procedure tForm1.arrayProcedure (globalArray: Array of TButton);
  2. begin
  3.    globalArray = // code to mess up with M, P or V
  4.  
  5.    return // the globalArray configuration to M, P or V
  6. end;
  7.  

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Set-Define / Get-Retrive array name
« Reply #4 on: December 05, 2017, 06:06:32 pm »
why you need the name? The name is something lost at compilation time. It is usually used only to help during writing the code not running it. For example you know how many rows and columns of buttons you have to create you can create an array and access the buttons based on their coordinates in that grid. eg.

Code: Pascal  [Select][+][-]
  1. var
  2.   MyButtons : array[0..9,0..4] of tbutton;//10x5 grid.
  3. begin
  4.    mybuttons[1,0] := TButton.create(self);
  5.    mybuttons[1,1] := TButton.create(self);
  6.    mybuttons[1,2] := TButton.create(self);
  7.    mybuttons[1,3] := TButton.create(self);
  8.    mybuttons[1,4] := TButton.create(self);
  9. end;
  10.  
that will create all the buttons for the second row and give you access to any button you need through
Code: Pascal  [Select][+][-]
  1. ....
  2. MyButtons[1,3].name := 'My_funky_name';
  3. MyButtons[1,3].Caption := 'Press me. You know you want to.';
  4. ....
  5.  

The name is for more "static conditions" and mostly there to help with the perception not the execution.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

douglas.cast

  • Guest
Re: Set-Define / Get-Retrive array name
« Reply #5 on: December 05, 2017, 06:42:07 pm »
why you need the name?

"Name" is a word I've used out to explain (at least try) what I need, you can call it: "identifier", "alias" or whatever you want and make sense to you, it's like call a "dog" of "animal", it just a "reference".

For example you know how many rows and columns of buttons you have to create you can create an array and access the buttons based on their coordinates in that grid. eg.

That's the point, there is no way to set a right or fixed value to the number of elements, it's a 0 to N:

The usual way is:
Code: Pascal  [Select][+][-]
  1. example : Array [0..10] of integer;
  2.  

In my application during runtime:
Code: Pascal  [Select][+][-]
  1. example Array [0..N]
  2.  
  3. // N = 1
  4. // N = 100
  5. // N = 57
  6. // N = some value
  7.  

The only thing I'm trying is share one procedure to 3 arrays.

Usualy when we use some component (TButton, TPanel) or some properties (Name, Align, ID), but I don't know how to "call" the array like a "component" to modify her.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.buttonExample (globalButton: TButton);
  2. begin
  3.    with globalButton do begin
  4.       Name:='Some name';
  5.       Parent:=panelA;
  6.       // and so on...
  7.    end;
  8. end;
  9.  
  10. // What I'm trying
  11. procedure TForm1.arrayExample (globalButton:TButton; globalArray: Array of TButton);
  12. var
  13.    b: TButton;
  14. begin
  15.    for b in globalArray // the array can be arrayMain, arrayPreview or arrayView
  16.    do begin
  17.       b.Font.Style:=[];
  18.    end;
  19.    globalButton.Font.Style:=[fsBold, fsItalic];
  20. end;
  21.  

If it's possible to do it I can call one procedure or function inside of a onClick event of a button and place some code in the button to get the result:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.buttonBClick(Sender:TObject);
  2. begin
  3.    arrayExample(buttonB, arrayMain);
  4. end;
  5.  
« Last Edit: December 05, 2017, 06:49:25 pm by douglas.cast »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Set-Define / Get-Retrive array name
« Reply #6 on: December 05, 2017, 07:47:39 pm »
"Name" is a word I've used out to explain (at least try) what I need, you can call it: "identifier", "alias" or whatever you want and make sense to you, it's like call a "dog" of "animal", it just a "reference".
Ok. I still do not see the problem.
For example you know how many rows and columns of buttons you have to create you can create an array and access the buttons based on their coordinates in that grid. eg.

That's the point, there is no way to set a right or fixed value to the number of elements, it's a 0 to N:
So? everyone has already pointed you to the common data structures you can use lists to dynamic arrays eg
Code: Pascal  [Select][+][-]
  1. type
  2.   TButtonArray = array of TButton;
  3. function CalculateArrayLength:Int64;
  4. begin
  5.    Result := 100000;
  6. end;
  7. var
  8.   MyUnknownArray : TButtonArray;
  9.   MyLength : int64;
  10.  
  11. begin
  12.   setLength(MyUnknownArray, CalculateArrayLength);
  13. end;
  14.  
Now you have an array of 100.000 buttons. Or what ever you want that number to be calculate at run time and do it.

The usual way is:
Code: Pascal  [Select][+][-]
  1. example : Array [0..10] of integer;
  2.  

In my application during runtime:
Code: Pascal  [Select][+][-]
  1. example Array [0..N]
  2.  
  3. // N = 1
  4. // N = 100
  5. // N = 57
  6. // N = some value
  7.  

The only thing I'm trying is share one procedure to 3 arrays.

Erm ok. You already have that with your ArrayExample method. Just pass which ever array you want as the second parameter. I guess I do not  see the problem again.

Usualy when we use some component (TButton, TPanel) or some properties (Name, Align, ID), but I don't know how to "call" the array like a "component" to modify her.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.buttonExample (globalButton: TButton);
  2. begin
  3.    with globalButton do begin
  4.       Name:='Some name';
  5.       Parent:=panelA;
  6.       // and so on...
  7.    end;
  8. end;
  9.  
  10. // What I'm trying
  11. procedure TForm1.arrayExample (globalButton:TButton; globalArray: Array of TButton);
  12. var
  13.    b: TButton;
  14. begin
  15.    for b in globalArray // the array can be arrayMain, arrayPreview or arrayView
  16.    do begin
  17.       b.Font.Style:=[];
  18.    end;
  19.    globalButton.Font.Style:=[fsBold, fsItalic];
  20. end;
  21.  

If it's possible to do it I can call one procedure or function inside of a onClick event of a button and place some code in the button to get the result:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.buttonBClick(Sender:TObject);
  2. begin
  3.    arrayExample(buttonB, arrayMain);
  4. end;
  5.  

And the code you showed so far does not work ? Any error messages so we can understand where you are stuck?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Set-Define / Get-Retrive array name
« Reply #7 on: December 05, 2017, 08:58:05 pm »
Here's an adaptation of earlier code to deal with three arrays (I think in the way you want).

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   SysUtils, Forms, Controls, Graphics, StdCtrls, Spin, ExtCtrls, typinfo;
  9.  
  10. type
  11.  
  12.   TArrayofButtons = array of TButton;
  13.  
  14.   TArrayKind = (akMain, akPreview, akView);
  15.  
  16.   TButtonArrays = array[TArrayKind] of TArrayofButtons;
  17.  
  18.   { TForm1 }
  19.  
  20.   TForm1 = class(TForm)
  21.     procedure FormCreate(Sender: TObject);
  22.   private
  23.     FButtonIndexSpinEdit: TSpinEdit;
  24.     FButtonArrayKindRadioGroup: TRadioGroup;
  25.     FButtonArrays: TButtonArrays;
  26.     procedure FButtonIndexSpinEditChange(Sender: TObject);
  27.     procedure SetButtonFontStyle(aButtonIndex: Integer; aButtonArray: TArrayofButtons);
  28.   end;
  29.  
  30. var
  31.   Form1: TForm1;
  32.  
  33. implementation
  34.  
  35. {$R *.lfm}
  36.  
  37. procedure TForm1.FormCreate(Sender: TObject);
  38. const
  39.   ButtonArrayLength = 5;
  40. var
  41.   i: Integer;
  42.   ba: TArrayKind;
  43.   s: String = '';
  44.  
  45.   function GetKind: String;
  46.   begin
  47.     Result:=GetEnumName(TypeInfo(TArrayKind), Ord(ba));
  48.     Delete(Result, 1, 2);
  49.   end;
  50.  
  51. begin
  52.   for ba:=Low(TArrayKind) to High(TArrayKind) do
  53.     begin
  54.       SetLength(FButtonArrays[ba], ButtonArrayLength);
  55.       for i:=0 to High(FButtonArrays[ba]) do begin
  56.         FButtonArrays[ba][i]:=TButton.Create(Self);
  57.         with FButtonArrays[ba][i] do begin
  58.           Caption:=Format('Btn%s%d',[GetKind, i]);
  59.           Left:=10 + Ord(ba)*75;
  60.           Top:=10 + i*30;
  61.           Parent:=Self;
  62.         end;
  63.       end;
  64.     end;
  65.   FButtonIndexSpinEdit:=TSpinEdit.Create(Self);
  66.   with FButtonIndexSpinEdit do begin
  67.     MaxValue:=ButtonArrayLength - 1;
  68.     Left:=250;
  69.     Top:=10;
  70.     OnChange:=@FButtonIndexSpinEditChange;
  71.     Parent:=Self;
  72.   end;
  73.   FButtonArrayKindRadioGroup:=TRadioGroup.Create(Self);
  74.   with FButtonArrayKindRadioGroup do begin
  75.     SetInitialBounds(250, 55, 100, 100);
  76.     BorderSpacing.Around:=10;
  77.     for ba in TArrayKind do begin
  78.       case ba of
  79.         High(TArrayKind): s:=s + GetKind;
  80.         else s:= s + GetKind + ',';
  81.       end;
  82.     end;
  83.     Items.CommaText:=s;
  84.     ItemIndex:=0;
  85.     Parent:=Self;
  86.   end;
  87.   AutoSize:=True;
  88. end;
  89.  
  90. procedure TForm1.FButtonIndexSpinEditChange(Sender: TObject);
  91. var
  92.   i: Integer;
  93.   ak: TArrayKind absolute i;
  94. begin
  95.   i:=FButtonArrayKindRadioGroup.ItemIndex;
  96.   if i > -1 then
  97.     SetButtonFontStyle(FButtonIndexSpinEdit.Value, FButtonArrays[ak]);
  98. end;
  99.  
  100. procedure TForm1.SetButtonFontStyle(aButtonIndex: Integer; aButtonArray: TArrayofButtons);
  101. var
  102.   b: TButton;
  103. begin
  104.   for b in aButtonArray do
  105.     b.Font.Style:=[];
  106.   if (aButtonIndex >= 0) and (aButtonIndex < Length(aButtonArray)) then
  107.     aButtonArray[aButtonIndex].Font.Style:=
  108.       aButtonArray[aButtonIndex].Font.Style + [fsBold, fsItalic];
  109. end;
  110.  
  111. end.

douglas.cast

  • Guest
Re: Set-Define / Get-Retrive array name
« Reply #8 on: December 06, 2017, 12:30:18 am »
Ok. I still do not see the problem.

I was explaining why I don't need any property called "name", I haven't understood what you said, I'm sorry.

So? everyone has already pointed you to the common data structures you can use lists to dynamic arrays eg

I'm kinda learning by myself (while I'm doing something to be more precise), and I don't have access or money to learn it on a school, so I don't know how to use them (by myself).

 I'll try learn the basics a little bit more. so But now I don't have that much knowledge to do all the stuff, my brain also does't help to much with programming.

Erm ok. You already have that with your ArrayExample method. Just pass which ever array you want as the second parameter. I guess I do not  see the problem again.

I've tryied to use this kind of structure to create the buttons dinamically, but the results are not stored inside of the "global array", not sure what or how I did it, but the results stay on the "procedure array" because I can't get the global array name / reference (or something like it), will be easier to understand (at least I supose so) with the example.

And the code you showed so far does not work ? Any error messages so we can understand where you are stuck?

I writed it in the browser just to ilustrate my exampleand "there is" no error because it's not a Lazarus or a compiler problem, it's a desing issue (the fact I don't know how to do this piece of code), maybe because I'm using a procedure instead of a function and I'm not returning the result to the rigth place or something like it, sorry once more.

The following code illustrate my "button-array problem", it create the buttons, but not inside of the global arrays, they stay empty even when being used like a reference, thats why I'm trying to get some kind of property or way to return the results to them, the onclose sigseverror and the N5 error stay like a mistery to me.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls;
  9.  
  10. type
  11.  
  12.   arrayButtons = Array of TButton;
  13.  
  14.   { TForm1 }
  15.  
  16.   TForm1 = class(TForm)
  17.     Button1: TButton;
  18.     Label1: TLabel;
  19.     M: TPanel;
  20.     procedure Button1Click(Sender: TObject);
  21.     procedure FormCreate(Sender: TObject);
  22.  
  23.   private
  24.     { private declarations }
  25.   public
  26.     { public declarations }
  27.     { CUSTOM PROCEDURES }
  28.     arrayMain,
  29.     arrayPreview,
  30.     arrayView    : arrayButtons;
  31.     l: Integer;
  32.  
  33.     procedure btnFontStyles(btn: TButton; boldArray: arrayButtons);
  34.     procedure clickMask(Sender:TObject);
  35.     procedure createButtons(arrayName:arrayButtons);
  36.   end;
  37.  
  38. var
  39.   Form1  : TForm1;
  40.   M      : TPanel;
  41.   i      : Integer;
  42.  
  43. implementation
  44.  
  45. {$R *.lfm}
  46.  
  47. { TForm1 }
  48.  
  49. procedure TForm1.FormCreate(Sender: TObject);
  50. var
  51.   i, t:Integer;
  52. begin
  53.   l:=0;
  54.  
  55.   SetLength(arrayMain,5);
  56.   createButtons(arrayMain);
  57.   //ShowMessage(IntToStr(Length(arrayMain)));
  58.   SetLength(arrayPreview,5);
  59.   createButtons(arrayPreview);
  60.   //ShowMessage(IntToStr(Length(arrayPreview)));
  61.   SetLength(arrayView,5);
  62.   createButtons(arrayView);
  63.   //ShowMessage(IntToStr(Length(arrayView)));
  64.  
  65. end;
  66.  
  67. procedure TForm1.Button1Click(Sender: TObject);
  68. begin
  69.   Label1.Caption:= Label1.Caption+'Main[0]: '       +arrayMain[0].Name+LineEnding;
  70.   Label1.Caption:= Label1.Caption+'Preview[0]: '    +arrayPreview[0].Name+LineEnding;
  71.   Label1.Caption:= Label1.Caption+'View[0]: '       +arrayView[0].Name+LineEnding;
  72. end;
  73.  
  74. procedure TForm1.btnFontStyles(btn: TButton; boldArray: arrayButtons);
  75. var
  76.   a, c:Integer;
  77.   b:TButton;
  78. begin
  79.   for c:=0 to High(boldArray) do begin
  80.     arrayMain[i].Font.Style:=[];
  81.     a:=a+1;
  82.   end;
  83.     btn.Font.Style:=[fsBold, fsItalic];
  84. end;
  85.  
  86. procedure TForm1.createButtons(arrayName: arrayButtons);
  87. var
  88.   t, c    :Integer;
  89.   b          :TButton;
  90. begin
  91.   t:=0;
  92.   SetLength(arrayName, 5);
  93.   for c:=0 to High(arrayName) do begin
  94.     arrayName[i]:= TButton.Create(Self);
  95.     with arrayName[i] do begin
  96.       //N should be the array name or some identifier to it, like "arrayMain1" instead of "N1"
  97.       Name:='N'+IntToStr(i);
  98.       Caption:=arrayName[i].Name;
  99.       Width:=100;
  100.       Height:=50;
  101.       Top:=t;
  102.       Left:=l;
  103.       Parent:=M;
  104.       t:=t+50;
  105.       i:=i+1;
  106.       OnClick:=@clickMask;
  107.       Label1.Caption:=Label1.Caption+'local array: '+Name+LineEnding;
  108.     end;
  109.   end;
  110.   CleanupInstance;
  111.   l:=l+110;
  112.   //Label1.Caption:=Label1.Caption+'arrayMain: '+arrayMain[0].Name+LineEnding;
  113. end;
  114.  
  115. procedure TForm1.clickMask(Sender: TObject);
  116. var
  117.   button:TButton;
  118. begin
  119.   button:=Sender as TButton;
  120.   {If I can, somehow put the arrayname on the above function, I'll be able to put it hier and control the selection of the array to play with its elements}
  121.   btnFontStyles(button, arrayMain);
  122. end;
  123.  
  124. end.
  125.  

I'm sorry for my delay and for my last post, you gave me a pretty good idea, work with a multidimensional array, so I can create the three "columns" to be the three different arrays that I have now and them create the button in the "lines" of each one (at least I compare a "2d" array with a table), in this way I can reference to the "collumn" instead of a array "name", that's a nice one.

Has I said, I'm sorry, I don't get the whole picture when I first read it, like I always avoid arrays I completely forgot it.

Any explanation of the above code and I can set the values to the 2d array will be great, thanks .

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Set-Define / Get-Retrive array name
« Reply #9 on: December 06, 2017, 01:21:26 am »
Perhaps you can draw a picture of what the panels/labels/buttons would look like so we can see what you are aiming at.
Or use RAD to mock up the control layout and post a screenshot.
Then we can help you code the behaviour you are after for the GUI you want.

jamie

  • Hero Member
  • *****
  • Posts: 6877
Re: Set-Define / Get-Retrive array name
« Reply #10 on: December 06, 2017, 02:24:30 am »
After read a mile of examples and descriptions here, I'll take a stab at it..  :D

 From what I gather he is looking to modify the global array from within a procedure
body, at least that is one of the ideas I am getting from this.

 Procedure DoSomethiingWithExternalArray(Var SomeSideArray:ARray of whatever);

 Is it possible he is looking for the difference of References and Values?

 I mean some of his post also indications he wishes to create a quick jump to a
segmented list of buttons in sorted order, which I guess could be done..


The only true wisdom is knowing you know nothing

douglas.cast

  • Guest
Re: Set-Define / Get-Retrive array name
« Reply #11 on: December 06, 2017, 12:04:40 pm »
After read a mile of examples and descriptions here, I'll take a stab at it..  :D

 From what I gather he is looking to modify the global array from within a procedure
body, at least that is one of the ideas I am getting from this.

 Procedure DoSomethiingWithExternalArray(Var SomeSideArray:ARray of whatever);

 Is it possible he is looking for the difference of References and Values?

 I mean some of his post also indications he wishes to create a quick jump to a
segmented list of buttons in sorted order, which I guess could be done..

And you got it, that's it.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Set-Define / Get-Retrive array name
« Reply #12 on: December 06, 2017, 05:45:27 pm »
Attached is an example of referring to arrays of buttons.

jamie

  • Hero Member
  • *****
  • Posts: 6877
Re: Set-Define / Get-Retrive array name
« Reply #13 on: December 07, 2017, 02:22:18 am »
I am still not sure which one he was after, quick index or the Reference/Value types
in a call.

 But to add to that... Controls have a TAG property which you are free to use . This
property can be set to an index that points within a global array bounds.

 This means that each Button has it's own TAG property, when you click it, you can
then access the array index that is desired by referencing the TAG property of that
button.
 
  So to save space etc..

 You can assign the same OnClick event to all   the buttons. When the event gets
called, you can use the "Sender" to know which button was actually clicked ..

  (Sender as Tbutton).TAG = ?

 So instead of having a event per button, you can have one event for all the buttons
and simply reference the SENDER to know which button actually sent it.

 Maybe that will help you cause some more
The only true wisdom is knowing you know nothing

douglas.cast

  • Guest
Re: Set-Define / Get-Retrive array name
« Reply #14 on: December 19, 2017, 03:27:20 pm »
Sorry my delay, I realy run out of time these past days.

Attached is an example of referring to arrays of buttons.

Nice coding, but I can't adapt it (that easy) to my project, for a couple of reasons.
1. I need a simple code, it will be a MIT / GPL app, almost all public will be a weekend "programmer" instead of a professional one like you and other people on the forum
2. Control the buttons based on panels existence is not what I need and create everything with code is too complex, considering my application interface.
3. Buttons have no name defined by the array name, and I'm kinda need it latter on to define some SQL stuff.

I am still not sure which one he was after, quick index or the Reference/Value types in a call.

I need both

Quick index for visual modifications and reference / values types for background coding

But to add to that... Controls have a TAG property which you are free to use . This property can be set to an index that points within a global array bounds.

Manualy defined? Or the pascal / lazarus already dot i?

This means that each Button has it's own TAG property, when you click it, you can then access the array index that is desired by referencing the TAG property of that button.
 
So to save space etc..

You can assign the same OnClick event to all   the buttons. When the event gets called, you can use the "Sender" to know which button was actually clicked ..

(Sender as Tbutton).TAG = ?

So instead of having a event per button, you can have one event for all the buttons and simply reference the SENDER to know which button actually sent it.

Maybe that will help you cause some more

I was (I am) avoind the tag property because I can control things with the array index anyway, but it will be a good shortcut in future.

Like I was thinking there isn't a way to "set" a name to a array or so, I'll study all codes a little bit more and see if I can find a start point.

 

TinyPortal © 2005-2018