* * *

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

jb007

  • Jr. Member
  • **
  • Posts: 73
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: 1796
  • 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: 73
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: 1796
  • 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: 73
Re: Procedure without an Object
« Reply #4 on: April 16, 2017, 07:31:08 pm »
Tnx, lainz!
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