* * *

Author Topic: Procedure without an Object  (Read 868 times)

jb007

  • Jr. Member
  • **
  • Posts: 75
Procedure without an Object
« on: April 14, 2017, 09:50:02 pm »
Hi!

First some things I use and understand!

An Object is placed on a forum and this Object got its Properties and Events.
This all is working fine and I mostly use the "OnClick".

In that option you can open a list so you can chose a Event (Procedure).

This means that Button1-onClick can use an event from e.g. Button2 etc etc...


In my programm I'm using procedures WITHOUT atached to an Object;
These procedures I can easely 'call' from anny where in my code, for example


procedure myCode; forward;
.
.
procedure myCode;
begin
   // code
end;
.
.
procedure TFMain.Button1Click(Sender: TObject);  // start COM
begin
  // code
  myCode;
end;

THIS WORKS FINE!


But how do I call an event in myCode?

Like this:

procedure myCode;
begin
   tfmain.button4.onClick.button4click;  // how to?
end;

Tried different other things like:
button4.onclick
etc etc...

Or should I just easely use invisible Objects?
So I can always choose an event from the list by onClick (or whatever).

Thansk in advance! Macel.






to DIY or not to DIY

lainz

  • Hero Member
  • *****
  • Posts: 1832
  • Nace una flor, todos los días sale el sol...
    • BGRABitmap
Re: Procedure without an Object
« Reply #1 on: April 14, 2017, 09:59:23 pm »
You can place new methods inside the class of TForm1 for example.
Then you can choose them from the list in the Object Inspector.

But answering your question, is like this:

Code: Pascal  [Select]
  1. procedure test();
  2. begin
  3.   Form1.Button1Click(nil);
  4. end;

But is not recommended, better add a new method in the class.

Go to here, private declarations:
Code: Pascal  [Select]
  1. TForm1 = class(TForm)
  2.     Button1: TButton;
  3.     procedure Button1Click(Sender: TObject);
  4.   private
  5.     { private declarations }
  6.   public
  7.     { public declarations }
  8.   end;

then type
procedure test();

and Ctrl + Shift + C

It will create automatically
Code: Pascal  [Select]
  1. procedure TForm1.test;
  2. begin
  3.  
  4. end;  


There you can call like this:

Code: Pascal  [Select]
  1. procedure TForm1.test;
  2. begin
  3.   Button1Click(nil);
  4. end;
   

jb007

  • Jr. Member
  • **
  • Posts: 75
Re: Procedure without an Object
« Reply #2 on: April 15, 2017, 11:49:40 am »
Thanks!

I was  a bit in the right direction but I deffinitly needed your help!

But somehow I have to use:

private
  { private declarations }
  procedure myCode;
           
implementation 

procedure TForm1.myCode;
begin
  button4click(nil);
end;



instead of:


private
  { private declarations }
  procedure TForm1.myCode;
           
implementation 

procedure TForm1.myCode;
begin
  button4click(nil);
end;







to DIY or not to DIY

lainz

  • Hero Member
  • *****
  • Posts: 1832
  • Nace una flor, todos los días sale el sol...
    • BGRABitmap
Re: Procedure without an Object
« Reply #3 on: April 15, 2017, 07:09:16 pm »
That's because TForm1 is the name of the class. Then you need to specify only the method name, not the class name again. Different is in the body of the method that must have the class name on it always.

jb007

  • Jr. Member
  • **
  • Posts: 75
Re: Procedure without an Object
« Reply #4 on: April 16, 2017, 07:31:08 pm »
Tnx, lainz!
to DIY or not to DIY

jb007

  • Jr. Member
  • **
  • Posts: 75
Re: Procedure without an Object
« Reply #5 on: May 01, 2017, 08:27:38 pm »
Things improve rapidly!

Controlling an ArduinoMega's TimerCounter/PWM by RS232!

Oscilloscope Video: https://www.youtube.com/watch?v=Ho0_UuN-0nk

Screenshot Lazarus:
to DIY or not to DIY

SkyKhan

  • Full Member
  • ***
  • Posts: 140
Re: Procedure without an Object
« Reply #6 on: May 01, 2017, 11:03:44 pm »
Why dont you use actions ?

Use TActionList* to define your user actions then you can bind them to (multiple) menu items and/or buttons and/or other components which have "Action" property.
And you can manually call them as "myAction.Execute;" wherever you like.

*last item on standard tab
I will always ignore you! Yes, you too ( except if you are topic owner )

Remy Lebeau

  • Full Member
  • ***
  • Posts: 232
    • Lebeau Software
Re: Procedure without an Object
« Reply #7 on: May 02, 2017, 01:30:40 am »
But answering your question, is like this:

Code: Pascal  [Select]
  1. procedure test();
  2. begin
  3.   Form1.Button1Click(nil);
  4. end;

Alternatively, call the TButton.Click() method, and let it call the OnClick handler if one is assigned:

Code: Pascal  [Select]
  1. procedure test();
  2. begin
  3.   Form1.Button1.Click;
  4. end;

But is not recommended, better add a new method in the class.

It is would best to de-couple your business logic from your UI code, eg:

Code: Pascal  [Select]
  1. TForm1 = class(TForm)
  2.   Button1: TButton;
  3.   Button4: TButton;
  4.   procedure Button1Click(Sender: TObject);
  5.   procedure Button4Click(Sender: TObject);
  6. private
  7.   { private declarations }
  8. public
  9.   { public declarations }
  10.   procedure DoSomething;
  11. end;
  12.  
  13. procedure myCode;
  14. begin
  15.   Form1.DoSomething;
  16. end;
  17.  
  18. procedure TFMain.Button1Click(Sender: TObject);
  19. begin
  20.   myCode;
  21. end;
  22.  
  23. procedure TFMain.Button4Click(Sender: TObject);
  24. begin
  25.   DoSomething;
  26. end;
  27.  
  28. procedure TFMain.DoSomething;
  29. begin
  30.   // code here
  31. end;
  32.  
« Last Edit: May 02, 2017, 01:37:39 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) open source project - Admin, Developer

jb007

  • Jr. Member
  • **
  • Posts: 75
Re: Procedure without an Object
« Reply #8 on: May 02, 2017, 08:48:49 pm »
Thank y'all!

Sure, I will keep your suggestions that in mind.

At the moment busy coding mikroPascal. Lazarus proceed in a couple of day.

Marcel.



to DIY or not to DIY

 

Recent

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