I'm very new to delphi, doing a project for my A level. When I run my code the images just don't show, I've looked everywhere and my teacher can't help me. Can anyone tell me what I'm missing?
const
Animal : array[0..6] of string = ('Bears','Dogs','Cats','Chickens','Horses','Cows','Monkeys');
ImagePaths : array [0..6] of string
= ('img0.JPG', 'img1.JPG', 'img2.JPG', 'img3.JPG', 'img4.JPG', 'img5.JPG',
'img6.JPG');
var i:integer;
Images : array [0..11] of TImage;
procedure LoadImages;
var
k,l:integer;
begin
Randomize;
k:=Random(11);
for l:= 0 to k do
begin
Images[l] := TImage.Create(nil);
Images[l].Picture.LoadFromFile(ImagePaths[i])
end
end;
procedure TForm4.FormCreate(Sender: TObject);
begin
randomize;
i:=random(6);
QuestionLbl.Caption:=Format('How many %s are there?',[Animal[i]]);
LoadImages;
end;
The idea is that a random number of images of the same randomly selected animal is displayed for a child to then count and input, if that helps. Much appreciate any help.
edit.
as this is only a prototype I have copied it all to a new application and this is all the code I didn't include:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,jpeg, ExtCtrls;
type
TForm1 = class(TForm)
QuestionLbl: TLabel;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
The same error is occurring and I'm afraid I'm too ignorant to follow what I'm sure were very clear instructions.
Why you just don't put your TImages on the form, and just LoadFromFile the ones you want to show ? Appear to me that would be easier.
But: what you trying to accomplish? From the code, I can imagine you were trying to show a number of images to people count them and answer the question...
So, if you add (and position) the 11 empty(no image) TImages in the form, you can do:
If you still need to create the TImages on the fly, just create the 12 TImages once on FormCreate (as in David's answer) and keep calling the LoadImages.
EDIT:
Some ideas, since you are learning.
Creating visual controls on-the-fly is a very boring(in my opinion, of course) task that involves:
Almost all those steps David Heffernan's answer show the code for it. But, unless you really need a dynamic layout, doing all those on design-time is more practical ;-)