Autor Beitrag
Seraph
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 163

Windows.:siXPack:.
Delphi7 Professional
BeitragVerfasst: Di 11.10.05 09:14 
Hey Leute!

Ich habe folgendes Problem:

Ich will ein Array of TImages mit unbestimmter Größe erstellen!
Funktioniert es?
Natürlich nicht.

Das ist mein Code:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
var
Images: Array of TImage;



procedure TForm1.Button1Click(Sender: TObject);
begin
  Images[1] := TImage.Create(Form1);
  Images[1].Picture.LoadFromFile('.\images\test.jpg');
  Images[1].Top := 0;
  Images[1].Left := 0;
  Images[1].Visible := true;
end;


Der Compiler meint schon bei der Zeile "Images[1] := TImage.Create(Form1);":
Zugriffsverletzung.

Kann mir jemand weiterhelfen? :(

Danke im Voraus
Seraph

_________________
Wer früher stirbt ist länger tot!
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Di 11.10.05 09:22 
Du musst zuerst die Größe des Arrays festlegen:
ausblenden Delphi-Quelltext
1:
setlength(Images,10);// oder halt soviel wie du brauchst					

Die Indizierung beginnt bei 0, d.h. Images[0] ist das erste Element in dem Array.

Und, um das nächste wahrscheinliche Problem gleich mit zu lösen: images[0].parent := Form1; darf man auch nicht vergessen.

_________________
We are, we were and will not be.
Seraph Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 163

Windows.:siXPack:.
Delphi7 Professional
BeitragVerfasst: Di 11.10.05 09:29 
user profile iconGausi hat folgendes geschrieben:
Und, um das nächste wahrscheinliche Problem gleich mit zu lösen: images[0].parent := Form1


Danke Man!

Funktioniert wunderbar! Bei C++ (Muttersprache) funzt das etwas anders, drum hatte ich keine Planung, was das angeht!

Nochmals Danke...

Für alle Anderen: Der Code lautet richtig:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
var  
Images: Array[0..7] of TImage;  

procedure TForm1.Button1Click(Sender: TObject);  
begin  
  Images[0] := TImage.Create(Form1);  
  Images[0].Parent := Form1;
  Images[0].Picture.LoadFromFile('Imagepfad');  
  Images[0].Top := 0;  
  Images[0].Left := 0;  
  Images[0].Visible := true;  
end;



Seraph

_________________
Wer früher stirbt ist länger tot!
Seraph Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 163

Windows.:siXPack:.
Delphi7 Professional
BeitragVerfasst: Di 11.10.05 15:14 
Aber was ist, wenn ich jetzt eine OnClick-Funktion auf eines der images
aufrufen will, wie soll das denn funktionieren? :roll:

Danke im Voraus!
Seraph

_________________
Wer früher stirbt ist länger tot!
Grishnak
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 221

Windows XP Home
Delphi 7 PE, Delphi 2005 PE
BeitragVerfasst: Di 11.10.05 15:26 
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
type
  TForm1 = class(TForm)
    [...]
    procedure ImagesArrayClick(Sender: TObject);
    [...]
  end;

[...]

var  
Images: Array[0..7] of TImage;  

[...]

procedure TForm1.Button1Click(Sender: TObject);  
begin  
  Images[0] := TImage.Create(Form1);  
  Images[0].Parent := Form1;
  Images[0].Picture.LoadFromFile('Imagepfad');  
  Images[0].Top := 0;  
  Images[0].Left := 0;  
  Images[0].Visible := true;  
  Images[0].Tag := 0; // bei Images[1] dann := 1 etc.
  Images[0].OnClick := ImagesArrayClick; // wird beim Drauf-Clicken ausgelöst
end;

[...]

procedure TForm1.ImagesArrayClick(Sender: TObject);
var
  index: integer;
begin
  index:=(Sender as TImage).Tag; // anhand des index auf eines der acht Images reagieren

  [...]
end;

_________________
Mach' etwas idiotensicher und irgendjemand erfindet einen besseren Idioten!
Seraph Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 163

Windows.:siXPack:.
Delphi7 Professional
BeitragVerfasst: Mi 12.10.05 06:38 
Es kompilliert!!!! Es funktioniert!!!!
:D
Einfach nur geil!


Dieses Thema ist dank euch beiden: Gausi & Grishnak
gottseidank erledigt!

Danke echt! Seit stolz auf euch... :wink:


Grüße
Seraph

_________________
Wer früher stirbt ist länger tot!