Recent

Author Topic: SOLVED: Call a procedure by button inside a form created at runtime  (Read 1111 times)

SaraT

  • Full Member
  • ***
  • Posts: 121
  • A little student
Hi again guys :)
All I want is t close when I do click in TSpeedButton but I don't know
how to call the click event if the form and the button are created at runtime.

Please, see the below code.
Many thanks for your time and help.

Code: Pascal  [Select][+][-]
  1. procedure Mainform.Excecute; //This procedure is excecuted by a button and works OK
  2. var
  3.  Form: TForm;
  4.  
  5. begin
  6.   Form := TForm.Create(Self); //We create a new form and works OK
  7.   with Form do
  8.   begin
  9.     Left := 350;
  10.     Height := 239;
  11.     Top := 250;
  12.     Width := 411;
  13.     BorderIcons := [biSystemMenu];
  14.     BorderStyle := bsSingle;
  15.     Caption := 'Test';
  16.     ClientHeight := 239;
  17.     ClientWidth := 411;
  18.     Color := 16314859;
  19.     Position := poDesktopCenter;
  20.     // --------------------------------------
  21.     with TSpeedButton.Create(Form) do //A button inside the form.
  22.     begin
  23.       Parent := Form;
  24.       Left := 318;
  25.       Height := 27;
  26.       Top := 198;
  27.       Width := 77;
  28.       Caption := 'Close';
  29.       OnClick := ???; //How to call a procedure to close the created form at runtime?
  30.     end;
  31.     // --------------------------------------
  32.   end;
  33. end;
« Last Edit: February 20, 2020, 09:41:51 pm by SaraT »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Call a procedure by button inside a form created at runtime
« Reply #1 on: February 20, 2020, 09:04:59 pm »
Hi!

Your question can be solved:
 OnClick := ???; //How to call a procedure to close the created form at runtime

Code: Pascal  [Select][+][-]
  1. procedure MainForm.SarasClick (Sender: TObject);
  2. begin
  3. showMessage (' Bye Everybody');
  4. close;
  5. end;
  6.  
  7.  
  8. .......
  9. OnClick := @SarasClick;
  10. ....
  11.  
  12.  
Happy coding!

Winni
« Last Edit: February 20, 2020, 09:07:12 pm by winni »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Call a procedure by button inside a form created at runtime
« Reply #2 on: February 20, 2020, 09:07:27 pm »
One approach would be
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Execute;
  2. begin
  3.   Form := TForm.Create(Self); //We create a new form and works OK
  4.   with Form do
  5.   begin
  6.     SetInitialBounds(350, 250, 411, 239);
  7.     BorderIcons := [biSystemMenu];
  8.     BorderStyle := bsSingle;
  9.     Caption := 'Test';
  10.     Color := 16314859;
  11.     Position := poDesktopCenter;
  12.     // --------------------------------------
  13.     with TSpeedButton.Create(Form) do //A button inside the form.
  14.     begin
  15.       SetInitialBounds(318, 198, 77, 27);
  16.       Caption := 'Close';
  17.       OnClick := @FormClose;
  18.       Parent := Form;
  19.     end;
  20.     ShowModal;
  21.   end;
  22. end;
  23.  
  24. procedure TForm1.FormClose(Sender: TObject);
  25. var
  26.   sb: TSpeedButton absolute Sender;
  27. begin
  28.   if Sender is TSpeedButton then
  29.   (sb.GetParentComponent as TForm).ModalResult := mrClose;
  30. end;

SaraT

  • Full Member
  • ***
  • Posts: 121
  • A little student
Re: Call a procedure by button inside a form created at runtime
« Reply #3 on: February 20, 2020, 09:41:24 pm »
One approach would be
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormClose(Sender: TObject);
  2. var
  3.   sb: TSpeedButton absolute Sender;
  4. begin
  5.   if Sender is TSpeedButton then
  6.   (sb.GetParentComponent as TForm).ModalResult := mrClose;
  7. end;
Many thanks Howard :)
The below code made my day xD
I don't how it works but it works.

PS: Thanks Winni.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: SOLVED: Call a procedure by button inside a form created at runtime
« Reply #4 on: February 24, 2020, 02:20:10 pm »
For this purpose you can think of absolute as a hard cast (in detail it means that sb is at the same location as Sender, though you can use a different type). So the following would be equivalent:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormClose(Sender: TObject);
  2. begin
  3.   if Sender is TSpeedButton then
  4.   (TSpeedButton(Sender).GetParentComponent as TForm).ModalResult := mrClose;
  5. end;

 

TinyPortal © 2005-2018