4
votes

This is my whole code:

    unit Unit1;

interface

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

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

var
  Form1: TForm1;
  Images: array[0..29,0..39] of TImage; //array
implementation

{$R *.dfm}
//form create
procedure TForm1.FormCreate(Sender: TObject);
var xx,yy: Integer; //local variables
begin
        for xx:=0 to 29 do
                for yy:=0 to 39 do
                        begin
                             Images[xx,yy]:=Timage.Create(Form1);
                             Images[xx,yy].Canvas.Rectangle(0,0,17,17);
                             Images[xx,yy].Left:=xx*16;
                             Images[xx,yy].Top:=yy*16;
                        end;
end;

end.

And I always get the error : "Project Project1.exe has raised the exception class EClassNotFound with message "TImage not found". Process stopped. Use step or run to continue "

I have tried other codes on the internet like:
Delphi: TImage.Create causes Access violation
http://www.delphi-central.com/tutorials/memory_game_2.aspx

Nothing helps! Why is this happening?

Thank you.

2
When you create the images, use TImage.Create( Self). Never use a form variable as a reference.LU RD
@TLama, suppose you are creating the form from a procedure with a local var of TForm1. What will happen ? The images will be tied to the global Form1 parameter and this is not a good practice. In fact very bad. The Images var should be declared within the form class.LU RD
@TLama, the other problem is that, if you create an instance of TForm1 on the fly (in code) and assign it to a variable of a different name, you're in trouble. For instance: var TheForm: TForm1;begin TheForm := TForm1.Create(nil); try TheForm.ShowModal; finally TheForm.Free; end;. If at the point of TheForm being constructed, if there's not already an instance of Form1 created, you get an AV. Always use Self, which will refer to the current instance of the object.Ken White

2 Answers

8
votes

Are you sure you get the exception at the line with TImage.Create? Could it be you have an invalid DFM file still containig a TImage instance which is missing from the TForm1 declaration?

Normally all classes used as children in a form or datamodule are automatically registered for streaming. As there is no TImage declared in the form and no other form of the application contains a TImage, there is no registration.

You can simply test by dropping a TImage onto the form.

1
votes

and if you want to show in form add this code to loop:

Images[xx,yy].Parent:= Self;