Autor Beitrag
Sylvus
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 195



BeitragVerfasst: Mi 27.02.08 14:44 
Hi Leute, wir sollen gerade für die Schule ein kleines Delphi Programm schreiben. Das ganze basiert darauf, dass man 1, 2 oder 3 Streichhölzer wegnehmen kann. Ich dachte mir, ich könnte das graphisch lösen indem ich die Bilder dann visible:true oder visible:false mache. aber mein Programm stürzt immer ab. Ich habs mal unter home.arcor.de/jspren.../hoelzer/hoelzer.rar hochgeladen. Wäre super wenn mir jemand helfen könnte. Danke Sylvus
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mi 27.02.08 14:48 
Hallo und :welcome:!

Sinnvoller ist es, relevanten Quellcode direkt hier im Forum zu posten, dann muss sich jemand, der Dir helfen will, nicht erst die Datei runterladen, entpacken, in Delphi öffnen, die richtige Stelle suchen, ... :zwinker:

Quellcode in Deinem Beitrag bitte in Delphi-Tags verpacken. Danke! :-)

Grüße
Christian

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Sylvus Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 195



BeitragVerfasst: Mi 27.02.08 15:14 
danke für das herzliche Willkommen. Ich werd einfach mal den gesammten Code hier posten, weil er zum ersten nicht lang ist und zum zweitem ich nicht weiß, wo der Fehler ist =)
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:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
unit schule;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Image1: TImage;
    Image2: TImage;
    Image3: TImage;
    Image4: TImage;
    Image5: TImage;
    Image6: TImage;
    Image7: TImage;
    Image8: TImage;
    Image9: TImage;
    Image10: TImage;
    Image11: TImage;
    Image12: TImage;
    Image13: TImage;
    Image14: TImage;
    Image15: TImage;
    Image16: TImage;
    Image17: TImage;
    Image18: TImage;
    Image19: TImage;
    Image20: TImage;
    Image21: TImage;
    Image22: TImage;
    Image23: TImage;
    Image24: TImage;
    Image25: TImage;
    Image26: TImage;
    Image27: TImage;
    Image28: TImage;
    Image29: TImage;
    Image30: TImage;
    Image31: TImage;
    Image32: TImage;
    Image33: TImage;
    Image34: TImage;
    Image35: TImage;
    Image36: TImage;
    Image37: TImage;
    Image38: TImage;
    Image39: TImage;
    Image40: TImage;
    Image41: TImage;
    Image42: TImage;
    Image43: TImage;
    Image44: TImage;
    Image45: TImage;
    Image46: TImage;
    Image47: TImage;
    Image48: TImage;
    Image49: TImage;
    Image50: TImage;
    Button5: TButton;
    procedure Button5Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);
var
 Images : array of TImage;
an,weg1,weg2,weg3,i: LongInt;
begin
an := strtoint(InputBox('Wie viel Streichhölzer sind vorhanden?', 'max.50', ''));
setLength(Images,an);

  for i := 0 to an do
  Images[i].Visible:=true;

end;
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mi 27.02.08 15:32 
Dein Array of Image steht doch in keinerlei Verbindung zu den vorhandenen Images. Vor allem aber musst Du die Elemente des Array auch initialisieren, die Images also erzeugen. Dabei macht es auch keinen Sinn, dass bei jedem Button-Klick zu tun.

Ich würde das Array also als Feld der Form anlegen, die Images ausschließlich im Code erzeugen (und im Array ablegen). Wie man Komponenten in einem Array verwaltet, habe ich hier mal beschrieben: www.delphi-library.d...verwalten_43052.html

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Sylvus Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 195



BeitragVerfasst: Mi 27.02.08 16:26 
urgh ja also es leuchtet ein, aber ich verstehs noch nicht so ganz. Ich hab das mal bei mir so gemacht:
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:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
unit schule;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    procedure Button5Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
   ImageArray: Array of TImage;
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);
var
 Images : array of TImage;
an,weg1,weg2,weg3,i: LongInt;
begin
an := strtoint(InputBox('Wie viel Streichhölzer sind vorhanden?', 'max.50', ''));
setLength(Images,an);

  for i := 0 to High(Images) do
 begin
    ImageArray[i] := TImage.Create(self);
    with ImageArray[i] do
    begin 
      parent := self; 
      left := 10; 
      top := i * 25; 
      name := IntToStr(i);
    end;
end;
end;


aber das funktioniert immer noch nicht, was habe ich den falsch verstanden, bzw. nicht richtig gemacht?
Und entschuldigung wegen den vielleicht doofen Fragen, mach seit einer Woche Delphi und das ist mein zweites Array :D
Thx Sylvus
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Mi 27.02.08 16:31 
a) Du brauchst nur eins der beiden Arrays. Schmeiß mal das, was du lokal in der Prozedur definiert hast, raus, sodass du nur noch mit ImageArray arbeitest.
b) Du musst da auch ein Bild reinladen, was sichtbar wird - sonst siehst du die Images nicht ;)
ausblenden Delphi-Quelltext
1:
Image.Picture.LoadFromFile('c:\MeinBild.bmp');					

Danach kannst du das Image mit Visible (un-)sichtbar machen.
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mi 27.02.08 16:34 
Erzeugen und laden musst Du die Bilder natürlich auch nur einmal, also z.B. im FormCreate-Ereignis.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Sylvus Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 195



BeitragVerfasst: Mi 27.02.08 16:40 
hey ja danke, hab erstmal den Fehler gefunden und das geht auch alles super, aber:
Kann ich Image.Picture.LoadFromFile('c:\MeinBild.bmp'); auch so abwandeln, dass er immer ein Bild in dem gleichem Ordner nimmt, also wenn ich das Programm z.B. Portable haben will.... und wo füge ich die Zeile dann ein?
Wohl nicht so, oder? (Edit doch so)^^

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
  for i := 0 to High(ImageArray) do
 begin
    ImageArray[i] := TImage.Create(self);
    with ImageArray[i] do
    begin
      Picture.LoadFromFile('c:\MeinBild.bmp');
      parent := self;
      left := 10;
      top := i * 25;
      name :='im' + IntToStr(i);
    end;
end;
Shorty
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 107

Win XP, Ubuntu 8.04
Delphi 7 Prof., Delphi 2005 Pers.
BeitragVerfasst: Mi 27.02.08 17:29 
Moin Sylvus!

Du bekommst den Pfad deines Programm durch ExtractFilePath(ParamStr(0)), wobei ParamStr(0) den Pfad und den Namen anzeigt und ExtractFilePath es auf den Pfad kürzt. Ich weiß jetzt aber nicht, ob am Ende von Pfad so ein Backslash ist, das müsstest du überprüfen...

Um nach 25 Zeilen eine neu Zeile anzufangen, musst du Top entsprechend deiner Schleifenvariable setzen, es könnte dann ungefähr so aussehen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
for i := 0 to High(ImageArray) do
  begin
    ImageArray[i] := TImage.Create(self);
    with ImageArray[i] do
      begin
        Picture.LoadFromFile(ExtractFilePath(ParamStr(0))+'MeinBild.bmp');
        Parent := self;
        Left := ((i - 1) mod 25) * Width;
        Top := ((i - 1) div 25) * Height;
        Name :='im' + IntToStr(i);
     end;
  end;

Zur Erklärung:
mod gibt den Rest einer Division, so wird Left entsprechend von 0 bis 24 gesetzt und div ist eine Ganzzahldivison, bei der immer abgerundet wird, also ist das Ergebnis von 0 - 24 immer 0, von 25 - 49 immer 1 usw.

MfG Shorty
Sylvus Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 195



BeitragVerfasst: Mi 27.02.08 17:32 
Super funktioniert alles, tolles Forum, tolle Member :D
Ich werd mal weiter machen, bis zur nächsten Frage :)
Grüße Sylvus