* * *

Author Topic: [SOLVED] How to assign event OnClick in this case?  (Read 289 times)

yurkad

  • Jr. Member
  • **
  • Posts: 85
[SOLVED] How to assign event OnClick in this case?
« on: April 27, 2017, 03:56:50 pm »
I have following Procedure in  Form1 :
Code: Pascal  [Select]
  1. procedure btn1Click;
  2. begin
  3.   showMessage('1');
  4. end;
The button btn1 will be created in other module - unit2,
I need assign event OnClick to btn1 within unit2, no within Form1.
In Form1 module is accessible variable BtnClick, which is defined in unit2:
Code: Pascal  [Select]
  1. Type
  2.   TBtnClick = Procedure;  
  3. Var BtnClick:TBtnClick;
I was trying to transmit event btn1Click thus:
In Form1:
Code: Pascal  [Select]
  1. BtnClick := @btn1Click;
In unit2:
Code: Pascal  [Select]
  1. btn1.OnClick := ^BtnClick;
Was error.

I was trying following variants too:
Code: Pascal  [Select]
  1. btn1.OnClick := BtnClick;
and
Code: Pascal  [Select]
  1. btn1.OnClick := @BtnClick;

Unsuccessfully.

Please, how to assign event OnClick in this case?
« Last Edit: April 27, 2017, 04:58:39 pm by yurkad »

Handoko

  • Hero Member
  • *****
  • Posts: 1015
  • My goal: build my own game engine using Lazarus
Re: How to assign event OnClick in this case?
« Reply #1 on: April 27, 2017, 04:02:31 pm »
Have you tried:

Code: Pascal  [Select]
  1. procedure btn1Click(Sender: TObject);
  2. begin
  3.   showMessage('1');
  4. end;

Also:

Code: Pascal  [Select]
  1. btn1.OnClick := @btn1Click;

And, it should be:

Code: Pascal  [Select]
  1. Type
  2.   TBtnClick = TNotifyEvent;
« Last Edit: April 27, 2017, 04:09:18 pm by Handoko »

yurkad

  • Jr. Member
  • **
  • Posts: 85
Re: How to assign event OnClick in this case?
« Reply #2 on: April 27, 2017, 04:15:01 pm »
I added (Sender: TObject) in both modules. Unsuccessfully.

About TNotifyEvent I do not know how change code in Form1 and in unit2.
Without change appear error.
« Last Edit: April 27, 2017, 04:19:25 pm by yurkad »

Handoko

  • Hero Member
  • *****
  • Posts: 1015
  • My goal: build my own game engine using Lazarus
Re: How to assign event OnClick in this case?
« Reply #3 on: April 27, 2017, 04:23:31 pm »
I ever had similar problem. But I can't find that code now. If I remember correctly, the new click procedure must be in a class not in a common procedure.

So, make this:

Code: Pascal  [Select]
  1. procedure btn1Click(Sender: TObject);

becomes:

Code: Pascal  [Select]
  1. procedure Something.btn1Click(Sender: TObject);

wp

  • Hero Member
  • *****
  • Posts: 3454
Re: How to assign event OnClick in this case?
« Reply #4 on: April 27, 2017, 04:29:09 pm »
TNotifyevent is declared as "procedure (Sender: TObject) of object":
  • "of object" means: the procedure must be a method, i.e. be declared within an object, not a standalone procedure.
  • "Sender: TObject" means: the event handler must be a procedure with a single parameter which is an object.
Your btn1Click procedure does not fulfill any of these requirements.

But BTW: what you describe does not sound like a good program logic. Clicks normally should be handled within the form containing the object affected.
Lazarus trunk / fpc 3.0.0 / Win32

yurkad

  • Jr. Member
  • **
  • Posts: 85
Re: How to assign event OnClick in this case?
« Reply #5 on: April 27, 2017, 04:45:53 pm »
Quote
what you describe does not sound like a good program logic. Clicks normally should be handled within the form containing the object affected.
You are right. The best is Assign event directly in Form1. In my case it is possible. But appear another problems, which I want to avoid.


Now:
I have following Procedure in  Form1 :
Code: Pascal  [Select]
  1. procedure TForm1.btn1Click(Sender: TObject);
  2. begin
  3.   showMessage('1');
  4. end;
  5. ...
  6. BtnClick  := @Btn1Click;
  7.  

in unit2:
Code: Pascal  [Select]
  1. Type
  2.   TBtnClick = TNotifyEvent;
  3. Var BtnClick:TBtnClick;
  4. ...
  5. btn1.OnClick := BtnClick ;
  6.  

Was compiled without errors ...
« Last Edit: April 27, 2017, 04:56:18 pm by yurkad »

wp

  • Hero Member
  • *****
  • Posts: 3454
Re: How to assign event OnClick in this case?
« Reply #6 on: April 27, 2017, 04:51:38 pm »
But the other point is that BtnClick must be implemented within a class. So, this will not work
Code: Pascal  [Select]
  1. procedure BtnClick(Sender: TObject);   // this is an ordinary procedure
  2. begin
  3.   ShowMessage('Clicked');
  4. end;
But this will
Code: Pascal  [Select]
  1. type
  2.   TSomeObject = class(...)
  3.     procedure BtnClick(Sender: TObject);
  4.     ...
  5.   end;
  6.  
  7. procedure TSomeObject.BtnClick(Sender: TObject);   // this is a method of class
  8. begin
  9.   ShowMessage('Clicked');
  10. end;
Lazarus trunk / fpc 3.0.0 / Win32

yurkad

  • Jr. Member
  • **
  • Posts: 85
Re: How to assign event OnClick in this case?
« Reply #7 on: April 27, 2017, 04:52:15 pm »
All is working.
Thank you very much, Handoko  :D

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus