Recent

Author Topic: Applying an Event Handler Programmatically  (Read 4476 times)

Cran0g

  • New Member
  • *
  • Posts: 28
Applying an Event Handler Programmatically
« on: June 08, 2011, 11:41:59 am »
Hi Folks,

I have created an array of images and can make any (or all) of them appear on the screen, so that's all sorted.  I have declared and written a procedure designed to catch the onMouseEnter event and I want to attach this procedure to each image in the array...

Code: [Select]
      procedure handleMenuMouseOver(Sender: TObject);
I've looked at examples in the forum but none of them seem to work.  At the moment, I am using a simple loop to iterate through all the elements of the image array, setting the attributes of each.  I wanted to say something like:

Code: [Select]
      menuTabs[i].onMouseEnter := handleMenuMouseOver(menuTabs[i]);
But it doesn't want to know.  I keep getting an error to say that the procedure part of the statement is untyped.  Can anyone help, please?

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: Applying an Event Handler Programmatically
« Reply #1 on: June 08, 2011, 11:54:07 am »
This should do it :)
Code: [Select]
menuTabs[i].onMouseEnter := @handleMenuMouseOver;

Cran0g

  • New Member
  • *
  • Posts: 28
Re: Applying an Event Handler Programmatically
« Reply #2 on: June 08, 2011, 01:38:57 pm »
Hi Laksen,

Thanks for that.  Yes, I saw that in a thread that came up when I was searching originally for a solution.  However, when I tried that before (and again, now), the debugger goes back to my original declaration of the procedure and says it's a forward declaration.  Just to let you know, the procedure is declared in the Form at the beginning and the loop to set up the images and their procedure is in the form's FormShow handler.

I'm sure you're on the right track, so what am I doing wrong in the declaration of the procedure?

Cran0g

  • New Member
  • *
  • Posts: 28
(Solved) Re: Applying an Event Handler Programmatically
« Reply #3 on: June 08, 2011, 02:46:33 pm »
Hi Laksen,

Your approach worked.  It was my fault in the definition of the procedure, I omitted to prefix the procedure name with "TForm."

Works fine now.  Thank you.

 

TinyPortal © 2005-2018