-1

My procedure for creating and loading picture into these images, but how do I create an OnClick procedure and determine which on I am clicking on?

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, jpeg, ExtCtrls, Math, StdCtrls;

type
  TForm1 = class(TForm)
Label1: TLabel;
procedure FormCreate(Sender: TObject);
private
procedure ImageClick(Sender: TObject);
{ Private declarations }
public
{ Public declarations }
end;

var
 Form1: TForm1;
 Box: array [1..8,1..8] of TImage;
 size: integer;

implementation


{$R *.dfm}

procedure imagecreation;
var i,j: integer;
begin
  size:=60;
  for i:=1 to 8 do
    begin
      for j:=1 to 8 do
        begin
      box[i,j]:=Timage.Create(Form1);
      box[i,j].AutoSize:=false;
      box[i,j].Proportional:=true;
      box[i,j].Width:=size;
      box[i,j].Height:=size;
      box[i,j].Top:=(i+1)*size;
      box[i,j].Left:=(j+1)*size;
      box[i,j].Parent:=Form1;
      box[i,j].OnClick:=ImageClick;
        end;
    end;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
  imagecreation;
end;

procedure TForm1.ImageClick(Sender: TObject);
var
  ClickedImage: TImage;
begin
  ClickedImage := Sender as TImage;
end;
end.

at which point it calls "ImageClick" and undeclared identifier. How are these declared?

Simon Davy
  • 1
  • 1
  • 2

2 Answers2

2

If you got a form or other class, you can define a method in there and attach it to all images:

// The event handler
procedure TForm1.ImageClick(Sender: TObject);
var
  Image: TImage;
begin
  // Senders points to the image clicked
  Image := Sender as TImage;
  // Use Image as you like. 
end;

// Creating the images.
procedure TForm1.CreateImage;
var
  Image: TImage;
begin
  Image := TImage.Create(Self);
  // Set parent, load picture, etc.
  Image.OnClick := ImageClick; // Attach the event.
end;
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • I tried it, but it calls ImageClick an undeclared identifier (I have it written exactly as you put it) and the Parent or Create does not work with Self as it calls it an undeclared identifier too. What could be the problem? – Simon Davy Dec 22 '12 at 18:33
  • @SimonDavy Add "procedure ImageClick(Sender: TObject);" to your form's object declaration in the "private" section. – A Lombardo Dec 22 '12 at 18:57
  • I did and it still calls it undeclared. This is strange. – Simon Davy Dec 22 '12 at 19:55
  • @SimonDavy It would be so much easier if you would stop hiding your code from us. Show it!! We can't see your screen. Don't make us guess. Show a minimal cut-down sample. – David Heffernan Dec 22 '12 at 20:00
  • The procedure that creates the images must be a method of the same form (not only in the same unit). Or if it isn't, you should reference the form instance as well, like: `Image.OnClick := Form1.ImageClick`. – GolezTrol Dec 22 '12 at 20:21
  • Thanks Golez, that seems to have solved it, you're a lifesaver :) – Simon Davy Dec 22 '12 at 20:28
1

Create a common OnClick handler (for instance, on your form):

procedure TForm1.ImageClick(Sender: TObject);
var
  ClickedImage: TImage;
begin
  ClickedImage := Sender as TImage;
  // Do whatever with the image clicked
end;

(Tip: Once you've typed the code above in the Code Editor, hit Ctrl+Shift+C, and the IDE will add the declaration to your form's interface automatically for you.)

When you create your image and assign it to the array, set it's OnClick event to the one above:

for i:=1 to 8 do 
begin 
  for j:=1 to 8 do 
  begin      
    Images[i, j] := TImage.Create(nil);
    Images[i, j].OnClick := ImageClick;
    Images[i, j].LoadFromFile(AnImageFilename);
    // Other settings for image
  end;
end;

EDIT: Now that you've posted your code, the problem is that you've failed to make imagecreation a method of your form, and therefore it doesn't know where to find ImageClick . You can fix it by changing your code from

procedure imagecreation;

to

procedure TForm1.imagecreation;

and using the shortcut I gave you earlier to add it to the interface declaration of your form.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • How come it calls it undeclared even if I did as you said? Btw - thanks for the shortcut :-) – Simon Davy Dec 22 '12 at 19:56
  • @Simon: If you followed instructions, it should work. If it doesn't, you need to show us your code so we don't have to guess. At the very least, provide the **actual, full error message** you're getting. – Ken White Dec 22 '12 at 20:09
  • [Error] Unit1.pas(46): Undeclared identifier: 'ImageClick' – Simon Davy Dec 22 '12 at 20:17
  • This actually is the whole code, I eddited it into the question about 15 minutes ago. – Simon Davy Dec 22 '12 at 20:27
  • I edited after you finally posted your code to give you a solution. See how much easier it was once you decided to supply the information? :-) – Ken White Dec 22 '12 at 20:33
  • In my experience, when I press Ctrl+Shift+C, the declaration is always added to the `private` section. Do you happen to know if that can be customised (in Delphi 6 in particular)? – Andriy M Dec 22 '12 at 20:54
  • @Andriy: No, for some reason it always goes to the `private` section, and I don't know of any way to customize it. I frequently add a method to something like a datamodule that I want to access outside it, try and use it, and then the compiler reminds me I have to go back and move it from `private` to `public`. – Ken White Dec 22 '12 at 20:57
  • Yes, that's a familiar situation. :) It's not a big deal for me really, just wondered if it *could* be changed. Thanks for your answer. – Andriy M Dec 22 '12 at 21:03