Recent

Author Topic: [SOLVED] Add Click Event To Button Array  (Read 3049 times)

pixelink

  • Hero Member
  • *****
  • Posts: 1260
[SOLVED] Add Click Event To Button Array
« on: April 24, 2019, 06:52:49 pm »
Hi,

I have a button array and can't figure out yet how to add a click even to each button.

CODE:

Code: Pascal  [Select][+][-]
  1.  
  2. procedure TForm1.btnGenerateClick(Sender: TObject);
  3. var
  4.   btn:Tbutton;
  5.   x: Integer;
  6.   W: Integer = 200;
  7.   H: Integer = 35;
  8.   T: Integer = 20;
  9.   L: Integer = 20;
  10.   FS: Integer = 12;
  11. begin
  12.  
  13.  For x:= 1 to 10 do
  14.   begin
  15.     btn:=TButton.create(nil);
  16.     btn.parent:=Form1;
  17.     btn.Font.Size:=FS;
  18.     btn.Caption:='Button ' + IntToStr(x);
  19.     btn.Width:=W;
  20.     btn.Height:=H;
  21.     btn.Top:=T;
  22.     btn.left:=L;
  23.     T:=T + 40;
  24.  end
  25. end;
  26.  
  27.  
« Last Edit: April 24, 2019, 08:29:28 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Add Click Event To Button Array
« Reply #1 on: April 24, 2019, 06:58:40 pm »
Code: Pascal  [Select][+][-]
  1.     btn.OnClick:=@BtnClickMethod;

where BtnClickMethod is method! of object (not just a procedure), declared as
Code: Pascal  [Select][+][-]
  1. procedure BtnClickMethod(Sender: TObject);
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: Add Click Event To Button Array
« Reply #2 on: April 24, 2019, 07:04:01 pm »
I already figured that out just before you posted.

So in the procedure.

I have this... but generates an error. I am working on it. I think my ";" and "()" aren't right

Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnClickEvent(Sender: TObject);
  2. begin
  3.  ShowMessage((Sender as TControl).Caption));
  4. end;
« Last Edit: April 24, 2019, 07:10:35 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: Add Click Event To Button Array
« Reply #3 on: April 24, 2019, 07:11:42 pm »
Okay... got it figured out....

Thanks for your help

Finished Code:

Code: Pascal  [Select][+][-]
  1. type
  2.   procedure btnClickEvent(Sender: TObject);
  3.  
  4. var
  5.   Form1: TForm1;
  6.   FButtons: array[1..10] of TButton;
  7.  
  8. implementation
  9.  
  10. {$R *.lfm}
  11.  
  12. { TForm1 }
  13.  
  14. procedure TForm1.btnGenerateClick(Sender: TObject);
  15. var
  16.   btn:Tbutton;
  17.   x: Integer;
  18.   W: Integer = 200;
  19.   H: Integer = 35;
  20.   T: Integer = 20;
  21.   L: Integer = 20;
  22.   FS: Integer = 12;
  23. begin
  24.  
  25.  For x:= 1 to 10 do
  26.   begin
  27.     btn:=TButton.create(nil);
  28.     btn.parent:=Form1;
  29.     btn.Font.Size:=FS;
  30.     btn.Caption:='Button ' + IntToStr(x);
  31.     btn.Width:=W;
  32.     btn.Height:=H;
  33.     btn.Top:=T;
  34.     btn.left:=L;
  35.     btn.OnClick := @btnClickEvent;
  36.     T:=T + 40;
  37.  
  38.  end
  39. end;
  40.  
  41. procedure TForm1.btnClickEvent(Sender: TObject);
  42. begin
  43.     ShowMessage((Sender as TControl).Caption);
  44. end;
  45.  
« Last Edit: April 24, 2019, 07:27:11 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: Add Click Event To Button Array
« Reply #4 on: April 24, 2019, 07:58:53 pm »
Oh, not so fast buddy (thats me)

I tried to do this with TSpeedButton, but it doens't like the TSpeedButton
Says it can't be found. Expecting TButton
Why??

Code: Pascal  [Select][+][-]
  1. btn:=TSpeedButton.create(nil);  
  2.  
« Last Edit: April 24, 2019, 08:07:04 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Add Click Event To Button Array
« Reply #5 on: April 24, 2019, 08:07:57 pm »
Do you have unit Buttons in uses?
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: Add Click Event To Button Array
« Reply #6 on: April 24, 2019, 08:18:14 pm »
Do you have unit Buttons in uses?

Yes, its there

I don't get it.

Using the same code that works except switching out TButton for TSpeedButton
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: Add Click Event To Button Array
« Reply #7 on: April 24, 2019, 08:23:11 pm »
What's weird, is that TSpeedButton is a TButton control (from it's family)
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: Add Click Event To Button Array
« Reply #8 on: April 24, 2019, 08:29:11 pm »
Okay... figured it out.
I didn't change the VAR to TSpeed Button. Although I changed all the others

I had "btn:TButton;" changed to "btn:TSpeedButton;"


CODE FOR SPEED BUTTON

Code: Pascal  [Select][+][-]
  1.  
  2. unit Unit1;
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Buttons;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.   btnGenerate: TButton;
  17.   procedure btnGenerateClick(Sender: TObject);
  18.   procedure btnClickEvent(Sender: TObject);
  19.   private
  20.  
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28.   {button array}
  29.   FButtons: array[1..10] of TSpeedButton;
  30.  
  31. implementation
  32.  
  33. {$R *.lfm}
  34.  
  35. { TForm1 }
  36.  
  37. procedure TForm1.btnGenerateClick(Sender: TObject);
  38. var
  39.   btn:TSpeedButton;
  40.   x: Integer;
  41.   W: Integer = 200;
  42.   H: Integer = 35;
  43.   T: Integer = 20;
  44.   L: Integer = 20;
  45.   FS: Integer = 12;
  46. begin
  47.  
  48.  For x:= 1 to 10 do
  49.   begin
  50.     btn:=TSpeedButton.create(nil);
  51.     btn.parent:=Form1;
  52.     btn.Font.Size:=FS;
  53.     btn.Flat:=True;
  54.     btn.Caption:='Button ' + IntToStr(x);
  55.     btn.Width:=W;
  56.     btn.Height:=H;
  57.     btn.Top:=T;
  58.     btn.left:=L;
  59.     btn.OnClick := @btnClickEvent;
  60.     T:=T + 40;
  61.  
  62.  end
  63. end;
  64.  
  65. procedure TForm1.btnClickEvent(Sender: TObject);
  66. begin
  67. //ShowMessage((Sender as TControl).Caption));
  68. ShowMessage((Sender as TControl).Caption);
  69. end;
  70.  
  71. end.
  72.  
  73.  

THANKS FOR HELPING :)
« Last Edit: April 24, 2019, 08:31:04 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Add Click Event To Button Array
« Reply #9 on: April 24, 2019, 11:11:07 pm »
What's weird, is that TSpeedButton is a TButton control (from it's family)

No, they are not: TButton is a TWinControl and TSpeedButton is a TGraphicControl. They don't join, in the hierarchy tree, until TControl.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: Add Click Event To Button Array
« Reply #10 on: April 24, 2019, 11:38:43 pm »
What's weird, is that TSpeedButton is a TButton control (from it's family)

No, they are not: TButton is a TWinControl and TSpeedButton is a TGraphicControl. They don't join, in the hierarchy tree, until TControl.

Oh. Okay. When I hover my mouse over the control (says "Buttons LCL Base)
I assumed it was of the Button family.
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Add Click Event To Button Array
« Reply #11 on: April 25, 2019, 12:17:55 am »
Oh. Okay. When I hover my mouse over the control (says "Buttons LCL Base)
I assumed it was of the Button family.

Look closer and you'll that it says: "Butttons, LCLBase" for TSpeedButton and TBitButton. That means that they are on the unit Buttons of the package LCLBase.

For TButton it says "StdCtrls, LCLBase", meaning: StdCtrls unit of package LCLBase.

But where they are declared and implemented has very little to do with what its inheritance tree looks like.

They are "family" only from the point of the view of the User Interface: all of them are some kind of "button" :)
« Last Edit: April 25, 2019, 12:20:31 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: Add Click Event To Button Array
« Reply #12 on: April 25, 2019, 02:45:29 am »
Oh. Okay. When I hover my mouse over the control (says "Buttons LCL Base)
I assumed it was of the Button family.

Look closer and you'll that it says: "Butttons, LCLBase" for TSpeedButton and TBitButton. That means that they are on the unit Buttons of the package LCLBase.

For TButton it says "StdCtrls, LCLBase", meaning: StdCtrls unit of package LCLBase.

cool. Always learning :)
Thanks

But where they are declared and implemented has very little to do with what its inheritance tree looks like.

They are "family" only from the point of the view of the User Interface: all of them are some kind of "button" :)
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

 

TinyPortal © 2005-2018