Recent

Author Topic: Assign functions to dynamic objects  (Read 3516 times)

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Assign functions to dynamic objects
« on: June 23, 2018, 02:34:26 am »
I created a TEdit at runtime:

Code: Pascal  [Select][+][-]
  1. CommandLine := TEdit.Create(nil); CommandLine.Parent:=form1;
  2. CommandLine.Left:=8;CommandLine.Top:=330;CommandLine.Height:=12;CommandLine.Width:=336;
  3. CommandLine.AutoSize:=true;CommandLine.Color:=clBlack;CommandLine.Font.Color:=clWhite;  

Now I want assign a function to a keyup event, and be able to read the key.

Code: Pascal  [Select][+][-]
  1. CommandLine.OnKeyUp := @OnCommandLineKeyUp;

This is my function:

Code: Pascal  [Select][+][-]
  1. Procedure OnCommandLineKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  2. Begin
  3. //if Key=VK_RETURN do whatever
  4. end;  

How i can send the value of the key? Thanks in advance

EDIT: Justt found this old topic:
https://forum.lazarus.freepascal.org/index.php?topic=6860.0

But i still receive the error:
mc_main.pas(81,48) Error: Incompatible types: got "<address of procedure(TObject;var Word;TShiftState);Register>" expected "<procedure variable type of procedure(TObject;var Word;TShiftState) of object;Register>"
« Last Edit: June 23, 2018, 03:31:31 am by torbente »
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Assign functions to dynamic objects
« Reply #1 on: June 23, 2018, 03:44:06 am »
But i still receive the error:
mc_main.pas(81,48) Error: Incompatible types: got "<address of procedure(TObject;var Word;TShiftState);Register>" expected "<procedure variable type of procedure(TObject;var Word;TShiftState) of object;Register>"
You're assigning a procedure to a method property. See that "of object" phrase. You OnCommandLineKeyUp must be part of a class, could be any, including but not limited to TForm1.

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: Assign functions to dynamic objects
« Reply #2 on: June 23, 2018, 03:57:23 am »
@torbente

Here is an example that works:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, LCLType;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     CommandLine: TEdit;
  17.     procedure Button1Click(Sender: TObject);
  18.     procedure OnCommandLineKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  19.   end;
  20.  
  21. var
  22.   Form1: TForm1;
  23.  
  24. implementation
  25.  
  26. {$R *.lfm}
  27.  
  28. { TForm1 }
  29.  
  30. procedure TForm1.Button1Click(Sender: TObject);
  31. begin
  32.   CommandLine            := TEdit.Create(Self);
  33.   CommandLine.Parent     := Form1;
  34.   CommandLine.Left       := 50;
  35.   CommandLine.Top        := 30;
  36.   CommandLine.Height     := 12;
  37.   CommandLine.Width      := 200;
  38.   CommandLine.AutoSize   := True;
  39.   CommandLine.Color      := clBlack;
  40.   CommandLine.Font.Color := clWhite;
  41.   CommandLine.OnKeyUp    := @OnCommandLineKeyUp;
  42. end;
  43.  
  44. procedure TForm1.OnCommandLineKeyUp(Sender: TObject; var Key: Word;
  45.   Shift: TShiftState);
  46. begin
  47.   if Key=VK_RETURN then
  48.     ShowMessage('You''ve pressed Enter!');
  49. end;
  50.  
  51. end.

Don't simply copy/paste my code because by you will learn nothing. You should compare yours with mine carefully so you can understand what you did wrong.

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Assign functions to dynamic objects
« Reply #3 on: June 23, 2018, 05:00:28 am »
Quote
Don't simply copy/paste my code because by you will learn nothing. You should compare yours with mine carefully so you can understand what you did wrong.

I declared Commandline as a var, not inside form1 class.
Good news, now i understand more about FP structure. Bad news, i will need recode all the other components i had as vars  %)

Thank you both so much
« Last Edit: June 23, 2018, 05:08:47 am by torbente »
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: Assign functions to dynamic objects
« Reply #4 on: June 23, 2018, 05:15:20 am »
I think you misunderstood the solution I gave you. See the code below:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, LCLType;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     procedure Button1Click(Sender: TObject);
  17.     procedure OnCommandLineKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  18.   end;
  19.  
  20. var
  21.   Form1: TForm1;
  22.   CommandLine: TEdit;
  23.  
  24. implementation
  25.  
  26. {$R *.lfm}
  27.  
  28. { TForm1 }
  29.  
  30. procedure TForm1.Button1Click(Sender: TObject);
  31. begin
  32.   CommandLine            := TEdit.Create(Self);
  33.   CommandLine.Parent     := Form1;
  34.   CommandLine.Left       := 50;
  35.   CommandLine.Top        := 30;
  36.   CommandLine.Height     := 12;
  37.   CommandLine.Width      := 200;
  38.   CommandLine.AutoSize   := True;
  39.   CommandLine.Color      := clBlack;
  40.   CommandLine.Font.Color := clWhite;
  41.   CommandLine.OnKeyUp    := @OnCommandLineKeyUp;
  42. end;
  43.  
  44. procedure TForm1.OnCommandLineKeyUp(Sender: TObject; var Key: Word;
  45.   Shift: TShiftState);
  46. begin
  47.   if Key=VK_RETURN then
  48.     ShowMessage('You''ve pressed Enter!');
  49. end;
  50.  
  51. end.

The code above also works. Your mistake wasn't on the line #22, but it is on the line #44.

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Assign functions to dynamic objects
« Reply #5 on: June 23, 2018, 05:34:50 am »
OMG! Yes, you are rigth, i did not declare the Tform1 (class) before the function name at the begining, but it was because originally the function was not declared in the same unit as form1 (neither the TEdit variable to be honest)
It will be nice some kind of document "good techniques to structure your program in FP using lazarus" or so; tips, common used sintaxys... etc etc

Once again, thank you very much.
(I just read your avatar; i created several games using AGS  8))
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: Assign functions to dynamic objects
« Reply #6 on: June 23, 2018, 05:45:47 am »
Hooray,  you got it. :D

Actually the compiler already told you what you did wrong. Unfortunately, not much people can't fully understand what it means:
Quote
Error: Incompatible types: got "<address of procedure(TObject;var Word;TShiftState);Register>" expected "<procedure variable type of procedure(TObject;var Word;TShiftState) of object;Register>"

i created several games using AGS

You create games too. You should participate the Game Contest 2018:
https://forum.lazarus.freepascal.org/index.php/topic,39495.0.html

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Assign functions to dynamic objects
« Reply #7 on: June 23, 2018, 06:04:12 am »
Quote
You create games too. You should participate the Game Contest 2018:

OffTopic: A game of life (https://bitstorm.org/gameoflife/) where the user can put permanent blocks, teletransport holes, multipliers... the posibilites are almost infinite. Sadly im now in 2 different projects.
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: Assign functions to dynamic objects
« Reply #8 on: June 25, 2018, 05:49:00 am »
OffTopic: A game of life (https://bitstorm.org/gameoflife/) where the user can put permanent blocks, teletransport holes, multipliers... the posibilites are almost infinite. Sadly im now in 2 different projects.

I loved that game.  Years ago, I did that game in Pascal.  I have no idea where the source code is, and I think I will do it again.
-ASB: https://www.BrainWaveCC.com/

Lazarus v2.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

 

TinyPortal © 2005-2018