OnClick event for dynamic button?

Before I begin, I must point out that no other post in this thread has helped me yet.

I have a dynamic button called btnApply

It is dynamically created in the frmSort dynamic form through the click event of the btnSort static button on the frmTable static form

In the global scope, var frmTable is declared

btnApply: TButton; Procedure btnApplyClick(Sender:TObject); //other vars 

Under btnSort when clicked

 //other code btnApply:= TButton.create(frmSort); //all its properties BtnApply.onclick:= btnApplyClick; //other code 

Then later

 Procedure btnApplyClick(Sender:TObject); Begin //it code it has to execute End; 

I get an error on the page "BtnApply.onclick: = btnApplyClick;" A line of incompatible types between a method pointer and a regular procedure

How do I do this job?

Thank you in advance

+5
source share
2 answers

Your btnApplyClick should be an object method. Since the button should be in the form, which will be useful in any case, make it the method of the form itself:

 type TfrmSort = class(TForm) // UI controls listed here public procedure btnApplyClick(Sender: TObject); end; implementation procedure TfrmSort.btnApplyClick(Sender: TObject); begin (Sender as TButton).Caption := 'You clicked me'; end; procedure TfrmSort.FormCreate(Sender: TObject); var Btn: TButton; begin Btn := TButton.Create(Self); Btn.Parent := Self; Btn.Top := 100; Btn.Left := 100; Btn.OnClick := btnApplyClick; end; 

If for some reason you cannot make it a form method (although I don’t see how it will look for visual control), you can make it a method of any object, for example:

 implementation // You must use StdCtrls in order to have the types available if // it not already in your uses clause type TDummyButtonClickObj = class class procedure ButtonClickHandler(Sender: TObject); end; { TDummyButtonClickObj } class procedure TDummyButtonClickObj.ButtonClickHandler(Sender: TObject); begin (Sender as TButton).Caption := 'You clicked me.'; end; procedure TForm2.FormCreate(Sender: TObject); begin with TButton.Create(Self) do begin Parent := Self; Top := 100; Left := 100; Caption := 'Click here'; OnClick := TDummyButtonClickObj.ButtonClickHandler; end; end; 
+13
source

As others have pointed out, the event handler must be a member of the class. This is what the event expects. However, a non-member procedure can also be used as an event handler. Only a few additional steps are required to configure.

  • Add an additional explicit parameter to account for the Self pointer:

     procedure btnApplyClick(Self: Pointer; Sender: TObject); 
  • Use the TMethod entry to assign a procedure to a button:

     var btnApply: TButton; M: TMethod; //other vars Procedure btnApplyClick(Self: Pointer; Sender: TObject); ... btnApply := TButton.create(frmSort); //all its properties M.Code := @btnApplyClick; M.Data := nil; // can be anything you want passed to the Self parameter BtnApply.onclick := TNotifyEvent(M); //other code ... procedure btnApplyClick(Self: Pointer; Sender: TObject); Begin // code to execute End; 
+8
source

Source: https://habr.com/ru/post/1234145/


All Articles