Autor Beitrag
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: Sa 25.04.15 00:07 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
procedure TForm1.FormDestroy(Sender: TObject);
var y,x : integer;

begin
  for y := 0 to N-1 do
   for x := 0 to N-1 do begin
    if not (FeldShape[x,y]=nilthen
      FeldShape.free;
    FeldShape[x,y] := nil;
   end;
end;


ist das dann so?
Boldar
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1555
Erhaltene Danke: 70

Win7 Enterprise 64bit, Win XP SP2
Turbo Delphi
BeitragVerfasst: Sa 25.04.15 00:12 
Das sieht doch erstmal ganz gut aus.
Hast du denn das hier schon gemacht:
user profile iconNarses hat folgendes geschrieben Zum zitierten Posting springen:
Ja, das hab ich mir schon gedacht. :D Lösung: In der FormCreate-Methode musst du den Maximalwert per Code setzen. :idea:

??
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: Sa 25.04.15 00:18 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.FormCreate(Sender: TObject);
var snr,y,x : integer;
    shape : TShape;
    Schieberegler : TScrollBar;
begin
[...]
  NMax := 24;
  Schieberegler.Max := NMax;
end;


ich hab mir das so gedacht aber si ganz stimmt das glaub ich noch nicht
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Sa 25.04.15 00:33 
Moin!

Zeig mal den ganzen Code, wir müssen mal wieder einen Basisabgleich machen. ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: Sa 25.04.15 09:00 
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:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  StdCtrls;

const
  Nmax = 35;

type
    TNachbarn = 0..8// es gibt 8 Nachbarn einer Zelle

    TRegel = set of TNachbarn; // Auswahl von Nachbaranzahlen

    TFeld = array[0..N-10..N-1of Boolean; // Aufbau des Feldes festlegen (2-dimensionales array)

    TFeldShape = array[0..Nmax-10..NMax-1of TShape;
  { TForm1 }

  TForm1 = class(TForm)
    btnRegeln: TButton;
    btnLeeren: TButton;
    ckAutomatik: TCheckBox;
    Ende: TButton;
    ScrollBar1: TScrollBar;

    Timer: TTimer;
    procedure btnLeerenClick(Sender: TObject);
    procedure btnLesenClick(Sender: TObject);
    procedure btnRechnenClick(Sender: TObject);
    procedure btnRegelnClick(Sender: TObject);
    procedure btnSchreibenClick(Sender: TObject);
    procedure ckAutomatikChange(Sender: TObject);
    procedure EndeClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ScrollBar1Change(Sender: TObject);
    procedure Shape1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure TimerTimer(Sender: TObject);
    procedure FormDestroy(Sender: TObject);

  private

  public
    FeldShape: TFeldShape;
    Feld: TFeld;
    Ueberleben: TRegel;
    Geburt: TRegel;
    function LebtNachbar(x, y: Integer): Boolean;
    function AnzahlLebenderNachbarn(const x, y: Integer): Integer;
  end;

var
  Form1: TForm1;

implementation

uses Unit2;
{$R *.lfm}

{ TForm1 }

procedure TForm1.Shape1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var  ThisShape: TShape;
begin
  ThisShape := (Sender as TShape);
  if (ThisShape.Brush.Color = clWhite) then
     ThisShape.Brush.Color := clBlack
  else
     ThisShape.Brush.Color := clWhite;
end;

procedure TForm1.ckAutomatikChange(Sender: TObject);
begin
    if ckAutomatik.Checked then begin // Haken reingeklickt
    btnLesenClick(Self); // Feld einlesen
    Timer.Enabled := True; // Timer einschalten
  end
  else // Haken rausgeklickt
    Timer.Enabled := False; // Timer ausschalten
end;

procedure TForm1.btnLesenClick(Sender: TObject);
var x, y, snr: Integer;
    Shape: TShape;
begin
    for y := 0 to N-1 do // von unten nach oben
        for x := 0 to N-1 do begin // von rechts nach links
            snr := x +y *N +1;
            Shape := TShape(FindComponent('Shape' +IntToStr(snr)));
            if (Shape.Brush.Color = clBlack) then
               Feld[x, y] := True
            else
               Feld[x, y] := False;
        end;
end;

procedure TForm1.btnLeerenClick(Sender: TObject);
var  x, y, snr: Integer;
    Shape: TShape;
begin
   for y := 0 to N-1 do
       for x := 0 to N-1 do
           if  (feld[x,y]) then
             feld[x,y] := false;

   for y := 0 to N-1 do
       for x := 0 to N-1 do begin
           snr := x +y *N +1;
           Shape := TShape(FindComponent('Shape' +IntToStr(snr)));
           if (feld[x,y]) then
              shape.brush.color := clblack
           else
             shape.brush.color := clwhite;
       end;
end;

procedure TForm1.TimerTimer(Sender: TObject);
begin
  btnrechnenClick(Self);
end;

 procedure TForm1.btnRechnenClick(Sender: TObject);
  var
    x, y: Integer;
    FeldNeu: TFeld;
begin
  for y := 0 to N-1 do
    for x := 0 to N-1 do
      if Feld[x, y] then
         FeldNeu[x, y] := (AnzahlLebenderNachbarn(x, y) in ueberleben)
      else
         FeldNeu[x, y] := (AnzahlLebenderNachbarn(x, y) in  geburt);
  Feld := FeldNeu;
  btnSchreibenClick(Self);
end;

procedure TForm1.btnRegelnClick(Sender: TObject);

  function GetRuleSet(const AName: String): TRegel;
    var
      CheckBox: TCheckBox;
      i: Integer;
  begin
    Result := [];
    for i := 0 to 8 do begin
    checkbox:= TCheckBox(Form2.FindComponent(aName +IntToStr(i)));
    if checkbox.checked then
      result := result + [i];

    end;

  end;

begin
  Form2.ShowModal;
  ueberleben := GetRuleSet('ckueberleben');
  geburt := GetRuleSet('ckgeburt');
end;

 function TForm1.AnzahlLebenderNachbarn(const x, y: Integer): Integer;
   var
     dx, dy: Integer;
 begin
   Result := 0;
   for dy := -1 to 1 do
     for dx := -1 to 1 do begin
      if  ((dx<>0or (dy<>0)) then
          if LebtNachbar(x +dx, y +dy) then
         Inc(Result);
     end;
 end;

function TForm1.LebtNachbar(x, y: Integer): Boolean;
begin
  Result := Feld[(x+N) mod N, (y+N) mod N];
end;

procedure TForm1.btnSchreibenClick(Sender: TObject);
var  x, y, snr: Integer;
    Shape: TShape;
begin
   for y := 0 to N-1 do
       for x := 0 to N-1 do begin
           snr := x +y *N +1;
           Shape := TShape(FindComponent('Shape' +IntToStr(snr)));
           if (feld[x,y]) then
              shape.brush.color := clblack
           else
             shape.brush.color := clwhite;
       end;

end;

procedure TForm1.EndeClick(Sender: TObject);
begin
  close;
end;

procedure TForm1.FormCreate(Sender: TObject);
var snr,y,x : integer;
    shape : TShape;
    Schieberegler : TScrollBar;
begin
  for y := 0 to N-1 do
   for x := 0 to N-1 do begin
    Shape := TShape.Create(Nil); // Shape-Objekt erzeugen, als Eigentümer das Formular (=Self) angeben
    Shape.Parent := Self; // das neue Shape soll auf dem Formular (=Self) angezeigt werden
    snr := x +y *N +1;
    Shape.Name := 'Shape'+inttostr(snr); // Name der Komponente
    Shape.Left := 10+x*25;
    Shape.Top := 80+y*25;
    Shape.Width := 25;
    Shape.Height := 25;
    Shape.OnMouseDown := @Shape1MouseDown; // so weist man eine Methode zu
    FeldShape[x,y] := Shape;
   end;
  ClientHeight := N*25+100;
  ClientWidth := N*25+20;
  NMax := 24;
  Schieberegler.Max := NMax;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var y,x : integer;

begin
  for y := 0 to N-1 do
   for x := 0 to N-1 do begin
    if not (FeldShape[x,y]=nilthen
      FeldShape.free;
    FeldShape[x,y] := nil;
   end;
end;


end.
das wäre dann so und halt die unit2 noch
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 26.04.15 12:51 
Moin!

Ah, danke. Da ist doch noch was zu tun:
  • Du hast die Konstante N in Nmax geändert, aber die anderen Deklarationen nicht angepasst (z.B. TFeld, das basiert noch auf N). Die Felder (arrays) müssen natürlich jetzt mit der Maximalgröße angelegt werden und dann später nur bis zur ausgewählten Größe genutzt. :idea:
  • Was auch gleich der zweite Punkt ist: du brauchst unterhalb von der public-Deklaration eine weitere Variable, sagen wir einfach "N: Integer;", in der wir uns die aktuell vom Benutzer eingestellte Größe merken.
  • Du hast einen Button-Handler "btnLeerenClick" angelegt. Der soll schätze ich mal das Feld leer machen. Der ist aber unnötig kompliziert ausgefallen... :zwinker:
cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 26.04.15 14:03 
aber das spielfeld leeren funktioniert ja trotzdem :p

hab das jetzt geändert und n in nmax umgeschrieben :)
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 26.04.15 17:57 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
aber das spielfeld leeren funktioniert ja trotzdem :p
Ich hab ja auch nicht behauptet, dass es nicht funktionieren würde - aber das ändert immer noch nix daran, dass der Code dafür überflüssig kompliziert ist. 8) Egal, das schauen wir uns später noch an.

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
hab das jetzt geändert und n in nmax umgeschrieben :)
Sehr schön. Allerdings sollte das hier
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
procedure TForm1.FormCreate(Sender: TObject);
var snr,y,x : integer;
    shape : TShape;
    Schieberegler : TScrollBar;
begin
  for y := 0 to N-1 do
  //...
  ClientWidth := N*25+20;
  NMax := 24;
  Schieberegler.Max := NMax;
end;
eigentlich mindestens eine Warnung im Compiler produzieren, dass du da den Wert einer Konstanten verändert möchtest. :shock: Das geht natürlich nicht, da du ja hier (oben):
ausblenden Delphi-Quelltext
1:
2:
const
  Nmax = 35;
den Wert für Nmax als Konstante (also unveränderlich) festlegst.

Dann würde ich noch vorschlagen, statt einer ScrollBox eine TrackBar zu verwenden (ist bei Delphi im Register Win32). Probier mal lieber diese Komponente aus. ;)

Und dann noch das hier:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure TForm1.FormDestroy(Sender: TObject);
  var
    y, x: Integer;
begin
  for y := 0 to Nmax-1 do
    for x := 0 to Nmax-1 do begin
      if Assigned(FeldShape[x,y]) then begin // mit Assigned() kann man prüfen, ob einer (Zeiger-)Variablen ein Wert zugewiesen ist
        FeldShape.Free; // Komponente freigeben
        FeldShape[x,y] := nil// Referenz entfernen
      end;
   end;
end;

Bau das mal ein/um und dann zeig nochmal den kompletten Code. :les:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 26.04.15 18:57 
so das wär dann glaub ich das:
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:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
 unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  StdCtrls, ComCtrls;

const
  Nmax = 35;

type
    TNachbarn = 0..8// es gibt 8 Nachbarn einer Zelle

    TRegel = set of TNachbarn; // Auswahl von Nachbaranzahlen

    TFeld = array[0..Nmax-10..Nmax-1of Boolean; // Aufbau des Feldes festlegen (2-dimensionales array)

    TFeldShape = array[0..Nmax-10..NMax-1of TShape;
  { TForm1 }

  TForm1 = class(TForm)
    btnRegeln: TButton;
    btnLeeren: TButton;
    ckAutomatik: TCheckBox;
    Ende: TButton;

    Timer: TTimer;
    TrackBar1: TTrackBar;
    procedure btnLeerenClick(Sender: TObject);
    procedure btnLesenClick(Sender: TObject);
    procedure btnRechnenClick(Sender: TObject);
    procedure btnRegelnClick(Sender: TObject);
    procedure btnSchreibenClick(Sender: TObject);
    procedure ckAutomatikChange(Sender: TObject);
    procedure EndeClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ScrollBar1Change(Sender: TObject);
    procedure Shape1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure TimerTimer(Sender: TObject);
    procedure FormDestroy(Sender: TObject);

  private

  public
    FeldShape: TFeldShape;
    Feld: TFeld;
    Ueberleben: TRegel;
    Geburt: TRegel;
    N: Integer;
    function LebtNachbar(x, y: Integer): Boolean;
    function AnzahlLebenderNachbarn(const x, y: Integer): Integer;
  end;

var
  Form1: TForm1;

implementation

uses Unit2;
{$R *.lfm}

{ TForm1 }

procedure TForm1.Shape1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var  ThisShape: TShape;
begin
  ThisShape := (Sender as TShape);
  if (ThisShape.Brush.Color = clWhite) then
     ThisShape.Brush.Color := clBlack
  else
     ThisShape.Brush.Color := clWhite;
end;

procedure TForm1.ckAutomatikChange(Sender: TObject);
begin
    if ckAutomatik.Checked then begin // Haken reingeklickt
    btnLesenClick(Self); // Feld einlesen
    Timer.Enabled := True; // Timer einschalten
  end
  else // Haken rausgeklickt
    Timer.Enabled := False; // Timer ausschalten
end;

procedure TForm1.btnLesenClick(Sender: TObject);
var x, y, snr: Integer;
    Shape: TShape;
begin
    for y := 0 to N-1 do // von unten nach oben
        for x := 0 to N-1 do begin // von rechts nach links
            snr := x +y *N +1;
            Shape := TShape(FindComponent('Shape' +IntToStr(snr)));
            if (Shape.Brush.Color = clBlack) then
               Feld[x, y] := True
            else
               Feld[x, y] := False;
        end;
end;

procedure TForm1.btnLeerenClick(Sender: TObject);
var  x, y, snr: Integer;
    Shape: TShape;
begin
   for y := 0 to N-1 do
       for x := 0 to N-1 do begin
           if  (feld[x,y]) then
             feld[x,y] := false;
           snr := x +y *N +1;
           Shape := TShape(FindComponent('Shape' +IntToStr(snr)));
           if not (feld[x,y]) then
             shape.brush.color := clwhite;
       end;
end;

procedure TForm1.TimerTimer(Sender: TObject);
begin
  btnrechnenClick(Self);
end;

 procedure TForm1.btnRechnenClick(Sender: TObject);
  var
    x, y: Integer;
    FeldNeu: TFeld;
begin
  for y := 0 to N-1 do
    for x := 0 to N-1 do
      if Feld[x, y] then
         FeldNeu[x, y] := (AnzahlLebenderNachbarn(x, y) in ueberleben)
      else
         FeldNeu[x, y] := (AnzahlLebenderNachbarn(x, y) in  geburt);
  Feld := FeldNeu;
  btnSchreibenClick(Self);
end;

var ckgeburt, ckueberleben : TCheckBox;
     i : integer;
begin
  form2.ShowModal;
  geburt := [];
  ueberleben := [];

  for i := 0 to 8 do begin
    ckgeburt := TCheckBox(Form2.FindComponent('ckgeburt' +IntToStr(i)));
    if ckgeburt.checked then
      geburt := geburt + [i];
  end;

  for i := 0 to 8 do begin
    ckueberleben := TCheckBox(Form2.FindComponent('ckueberleben' +IntToStr(i)));
    if ckueberleben.checked then
      ueberleben := ueberleben + [i];
  end;
end;}
procedure TForm1.btnRegelnClick(Sender: TObject);

  function GetRuleSet(const AName: String): TRegel;
    var
      CheckBox: TCheckBox;
      i: Integer;
  begin
    Result := [];
    for i := 0 to 8 do begin
    checkbox:= TCheckBox(Form2.FindComponent(aName +IntToStr(i)));
    if checkbox.checked then
      result := result + [i];

    end;

  end;

begin
  Form2.ShowModal;
  ueberleben := GetRuleSet('ckueberleben');
  geburt := GetRuleSet('ckgeburt');
end;

 function TForm1.AnzahlLebenderNachbarn(const x, y: Integer): Integer;
   var
     dx, dy: Integer;
 begin
   Result := 0;
   for dy := -1 to 1 do
     for dx := -1 to 1 do begin
      if  ((dx<>0or (dy<>0)) then
          if LebtNachbar(x +dx, y +dy) then
         Inc(Result);
     end;
 end;

function TForm1.LebtNachbar(x, y: Integer): Boolean;
begin
  Result := Feld[(x+N) mod N, (y+N) mod N];
end;

procedure TForm1.btnSchreibenClick(Sender: TObject);
var  x, y, snr: Integer;
    Shape: TShape;
begin
   for y := 0 to N-1 do
       for x := 0 to N-1 do begin
           snr := x +y *N +1;
           Shape := TShape(FindComponent('Shape' +IntToStr(snr)));
           if (feld[x,y]) then
              shape.brush.color := clblack
           else
             shape.brush.color := clwhite;
       end;

end;

procedure TForm1.EndeClick(Sender: TObject);
begin
  close;
end;

procedure TForm1.FormCreate(Sender: TObject);
var snr,y,x : integer;
    shape : TShape;
    trackbar : TTrackbar;
begin
  for y := 0 to N-1 do
   for x := 0 to N-1 do begin
    Shape := TShape.Create(Nil); // Shape-Objekt erzeugen, als Eigentümer das Formular (=Self) angeben
    Shape.Parent := Self; // das neue Shape soll auf dem Formular (=Self) angezeigt werden
    snr := x +y *N +1;
    Shape.Name := 'Shape'+inttostr(snr); // Name der Komponente
    Shape.Left := 10+x*25;
    Shape.Top := 80+y*25;
    Shape.Width := 25;
    Shape.Height := 25;
    Shape.OnMouseDown := @Shape1MouseDown; // so weist man eine Methode zu
    FeldShape[x,y] := Shape;
   end;
  ClientHeight := N*25+100;
  ClientWidth := N*25+20;
  trackbar.Max := NMax;
end;

procedure TForm1.FormDestroy(Sender: TObject);
  var
    y, x: Integer;
begin
  for y := 0 to Nmax-1 do
    for x := 0 to Nmax-1 do begin
      if Assigned(FeldShape[x,y]) then begin // mit Assigned() kann man prüfen, ob einer (Zeiger-)Variablen ein Wert zugewiesen ist
        FeldShape.Free; // Komponente freigeben
        FeldShape[x,y] := nil// Referenz entfernen
      end;
   end;
end;


end.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 26.04.15 19:22 
Moin!

Zwei Kleinigkeiten haben wir aber noch:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure TForm1.FormDestroy(Sender: TObject);
//...
      if Assigned(FeldShape[x,y]) then begin // mit Assigned() kann man prüfen, ob einer (Zeiger-)Variablen ein Wert zugewiesen ist
        FeldShape[x,y].Free; // Komponente freigeben
        FeldShape[x,y] := nil// Referenz entfernen
//...
end;
Hier haben wir "vergessen" zu sagen, welches Shape da freigegeben werden soll. :oops: (das hätte eigentlich als Fehler im Kompiler auftauchen müssen :?)

Und hier:
ausblenden 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:
procedure TForm1.FormCreate(Sender: TObject);
var snr,y,x : integer;
    shape : TShape;
    trackbar : TTrackbar;
begin
  trackbar.Max := NMax; // nach oben verschoben
  N := TrackBar.Position; // N vorbelegen (wird ja gleich benutzt)
  ClientHeight := N*25+100// nach oben verschoben
  ClientWidth := N*25+20;
  for y := 0 to N-1 do
   for x := 0 to N-1 do begin
    Shape := TShape.Create(Nil); // Shape-Objekt erzeugen, als Eigentümer das Formular (=Self) angeben
    Shape.Parent := Self; // das neue Shape soll auf dem Formular (=Self) angezeigt werden
    snr := x +y *N +1;
    Shape.Name := 'Shape'+inttostr(snr); // Name der Komponente
    Shape.Left := 10+x*25;
    Shape.Top := 80+y*25;
    Shape.Width := 25;
    Shape.Height := 25;
    Shape.OnMouseDown := @Shape1MouseDown; // so weist man eine Methode zu
    FeldShape[x,y] := Shape;
   end;
end;
haben wir noch nicht dafür gesorgt, dass beim Programmstart N auch einen "vernünftigen" Wert erhält. Weiterhin habe ich das mal etwas umsortiert, so dass es mehr "Sinn" macht.

Damit sollte sich das wieder kompilieren lassen und auch korrekt laufen. OK? :les:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 26.04.15 19:35 
ich krieg da grad so ne komische fehlermeldung o.O
fehlermeldung
ne das war die falsche merk ich grad ich such mal die richtige :D

so das müsste die richtige sein

fehlermeldung2

Moderiert von user profile iconNarses: Beiträge zusammengefasst.
Einloggen, um Attachments anzusehen!
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 26.04.15 19:44 
Moin!

Ähm, ich seh da grad was: :?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure TForm1.FormCreate(Sender: TObject);
var snr,y,x : integer;
    shape : TShape;
    trackbar : TTrackbar;
begin
  trackbar.Max := NMax; // nach oben verschoben
  N := TrackBar.Position; // N vorbelegen (wird ja gleich benutzt)
//...
Das ist natürlich Blödsinn, da einfach eine Trackbar als lokale Variable anzulegen. Du musst natürlich die entsprechende Komponente auf das Formular legen (und dann sollte da ein "TrackBar1" automatisch erstellt werden). :idea:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 26.04.15 19:49 
also dann so
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
 procedure TForm1.FormCreate(Sender: TObject);
var snr,y,x : integer;
    shape : TShape;
begin
  trackbar1.Max := NMax; // nach oben verschoben
  N := TrackBar1.Position; // N vorbelegen (wird ja gleich benutzt)
  ClientHeight := N*25+100// nach oben verschoben
  ClientWidth := N*25+20;


das öffnet sich jetzt wieder und hat dann nen 3x3 feld aber mehr will das nicht machen
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 26.04.15 19:54 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
also dann so
Jup. ;)

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
das öffnet sich jetzt wieder und hat dann nen 3x3 feld aber mehr will das nicht machen
Dann stell mal TrackBar1.Position im Objekt-Inspektor auf 10, dann sollte der wieder mit 10 Feldern Kantenlänge starten.

Dass da nicht mehr passiert, ist erstmal normal. Genau da müssen wir ja noch ran. :zwinker:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 26.04.15 19:57 
ja,jetzt sind es 10x10 felder :)
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 26.04.15 20:00 
Moin!

Vorschläge, wie´s jetzt weiter geht? :lupe: :D

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 26.04.15 20:03 
irgendwie muss ich jetzt während das programm läuft N wieder einlesen aber wie weiß ich leider gar nicht :(
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 26.04.15 20:08 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
irgendwie muss ich jetzt während das programm läuft N wieder einlesen aber wie weiß ich leider gar nicht :(
Naja, das ist ja nicht besonders schwer, das hast du sogar schon gemacht:
ausblenden Delphi-Quelltext
1:
N := TrackBar1.Position;					

Allerdings... :? das reicht nicht. :nixweiss: Was muss denn noch alles geändert werden? :lupe: :gruebel:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 26.04.15 20:11 
die shapes müssen noch dementsprechen erstellt werden
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 26.04.15 20:16 
Moin!

Jap, und was heißt das im Detail? (beschreib mal so genau wie möglich - also was da ablaufen muss, den Code machen wir dann schon) :les:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.