Recent

Author Topic: [SOLVED] Custom procedure button click  (Read 588 times)

Pe3s

  • Hero Member
  • *****
  • Posts: 573
[SOLVED] Custom procedure button click
« on: September 10, 2023, 09:25:01 am »
Hello, how can I refer to a button in my own procedure?

Code: Pascal  [Select][+][-]
  1. SpeedButton1Click(Sender);
  2.  
« Last Edit: September 10, 2023, 04:57:15 pm by Pe3s »

Josh

  • Hero Member
  • *****
  • Posts: 1344
Re: Custom procedure button click
« Reply #1 on: September 10, 2023, 12:32:37 pm »
just add the sender ie
SpeedButton1Click(SpeedButton1);

note if your code is not in the form class then
form1.SpeedButton1Click(form1.SpeedButton1);

personally i would move the button events outside of the form controls; that way you seperate the logic and code form the gui, and is easier to maintain.
ie
Code: Pascal  [Select][+][-]
  1. procedure mybuttonclickhandler(sender:tobject);
  2. begin
  3.   if sender is tspeedbutton then do
  4.   begin
  5.     // code if tspeedbutton
  6.     // or a seperate procedure
  7.   end
  8.   else
  9.   if sender is tbutton then do
  10.   begin
  11.      // code if tbutton
  12.     // or a seperate procedure
  13.   end
  14.   else
  15.   if sender is tbcbutton then do
  16.   begin
  17.      // code if tbcbutton
  18.     // or a seperate procedure
  19.   end;
  20. end;


projeect below one speedbutton one button, and the onlcik events
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Buttons, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     SpeedButton1: TSpeedButton;
  17.     procedure Button1Click(Sender: TObject);
  18.     procedure SpeedButton1Click(Sender: TObject);
  19.   private
  20.     procedure testa;
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. // if calling from function/procedure not in tform
  35. procedure testb;
  36. begin
  37.   form1.SpeedButton1Click(form1.button1);
  38. end;
  39.  
  40. // callinf from proc/function that is part of tform
  41. procedure TForm1.testa;
  42. begin
  43.   SpeedButton1Click(SpeedButton1);
  44. end;
  45.  
  46. procedure TForm1.SpeedButton1Click(Sender: TObject);
  47. begin
  48.   if sender is tspeedbutton then
  49.   begin
  50.     showmessage('hello');
  51.   end
  52.   else
  53.   begin
  54.     showmessage('Ooops');
  55.   end;
  56. end;
  57.  
  58. procedure TForm1.Button1Click(Sender: TObject);
  59. begin
  60.   testa;    // should say hello as sending speedbutton1
  61.   testb;    // should say oops as sendinf button1
  62. end;
  63.  
  64. end.
  65.                              
« Last Edit: September 10, 2023, 12:40:49 pm by Josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Pe3s

  • Hero Member
  • *****
  • Posts: 573
Re: Custom procedure button click
« Reply #2 on: September 10, 2023, 04:56:55 pm »
@Josh, Thank you :)

 

TinyPortal © 2005-2018