Autor Beitrag
delphistart
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 119



BeitragVerfasst: Fr 08.12.06 15:44 
Ich habe folgendes Problem:

Ich heabe eine Datei in der sich Datensätze befinden. Ich weis nicht wieviele, daher erfrage ich dies und speichere die Zahl in ein Variable. Nun muss ich zu jedem Datensatz ein Textlabel, ein Editfeld, ein Button und 2 Checkbocken erzeugen. Wie kann ich das machen, wenn ich nicht weis ob ich 10 oder 100 oder ... Datensätze habe?

Danke für eure Hilfe!
AutopilotON
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17

Win 98,ME,XP SP2,Suse 10.0
D6, TurboDelphi 2006
BeitragVerfasst: Fr 08.12.06 15:53 
Habs selber noch nicht probiert, aber ich könnte mir vorstellen, dass das mit einem
dynamischen Array sehr gut geht.
bsp. für Edits:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
var arrEdit:array of TEdit;
    i:integer;
begin
 setlength(arrEdit,10);
 for i := 0 to 9 do begin
   arrEdit[i]:=TEdit.Create(self);
   arrEdit[i].Parent:=Form1;
   ....  
 end;
end;


Das dann für alle anderen Elemente und das Problem sollte gelöst sein^^
freak4fun
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 604
Erhaltene Danke: 4

Win 7 Pro
VS 2013 Express, Delphi, C#, PHP, Java
BeitragVerfasst: Fr 08.12.06 16:03 
Man sollte aber am Ende das FreeAndNil(); bzw. Free; nicht vergessen. ;)

MfG
freak

_________________
"Ich werde auf GAR KEINEN Fall…!" - "Keks?" - "Okay, ich tu's."
i++; // zaehler i um 1 erhoehen
delphistart Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 119



BeitragVerfasst: Sa 09.12.06 20:15 
user profile iconfreak4fun hat folgendes geschrieben:
Man sollte aber am Ende das FreeAndNil(); bzw. Free; nicht vergessen. ;)

MfG
freak


Wo muss ich denn das Free und wo das FreeAndNil() setzen? Und wann nehme ich welchen der beide Befehle?
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Sa 09.12.06 20:26 
Wenn du sie nicht mehr brauchst. FreeAnNil ist eine Funktion die Fre aufruft und die Referenz anschließend auf Nil setzt.

_________________
Markus Kinzler.
freak4fun
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 604
Erhaltene Danke: 4

Win 7 Pro
VS 2013 Express, Delphi, C#, PHP, Java
BeitragVerfasst: Sa 09.12.06 20:33 
user profile icondelphistart hat folgendes geschrieben:
user profile iconfreak4fun hat folgendes geschrieben:
Man sollte aber am Ende das FreeAndNil(); bzw. Free; nicht vergessen. ;)

MfG
freak


Wo muss ich denn das Free und wo das FreeAndNil() setzen? Und wann nehme ich welchen der beide Befehle?


Wenn du die Variablen nur in einer Funktion/Prozedur hast, dann am Ende dieser, sondt beim schließen des Programms. ;)

MfG
freak

_________________
"Ich werde auf GAR KEINEN Fall…!" - "Keks?" - "Okay, ich tu's."
i++; // zaehler i um 1 erhoehen
delphistart Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 119



BeitragVerfasst: Sa 09.12.06 20:36 
Ich abe jetzt mal proboert das alles umzusetzen, aber nun bekomme ich folgende Fehlermeldung:

Im Projekt Projekt1.exe ist ein Exception der Klasse EAccessViolation mit der Meldung 'Zugriffsverletzung bei Adresse 00464F15 in Modul 'Projekt1.exe'. Schreiben von Adresse 00000000' aufgetreten.

Hier mal mein Queltext:
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:
unit Unit11;

interface

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

type
  TArtikelliste = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  procedure element;
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Artikelliste: TArtikelliste;

implementation
 
{$R *.dfm}

procedure TArtikelliste.element;
  var arrEdit:array of TEdit;
      i:integer;
  begin
    for i:=0 to 9 do begin
    arrEdit[i]:=TEdit.Create(self);
    arrEdit[i].Parent:=artikelliste;
    arrEdit[i].Top:=50;
    arrEdit[i].Left:=50;
    arrEdit[i].Width:=50;
    end;
    freeandNil(arrEdit);
  end;
procedure TArtikelliste.Button1Click(Sender: TObject);
begin
  element;
end;

end.
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Sa 09.12.06 20:41 
1. arrEdit ist ein Array, du mußt also jedes element freigeben arrEdit[0]...arrEdit[9].
2. An dieser Stelle macht die Freigabe keinen Sinn.

_________________
Markus Kinzler.
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Sa 09.12.06 20:43 
1. Du hast dem dynamischen Array noch keine Länge zugewiesen. Deshalb endet jeder Zugriffsversuch mit einer Acces-Violation. Das macht man mit Setlength(arrEdit,9).
2. Setze FreeAndNil lieber ins OnDestroy deiner Hauptform.

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
delphistart Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 119



BeitragVerfasst: Sa 09.12.06 20:46 
Habe das jetzt mal so umgestelt:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
procedure TArtikelliste.element;
  var arrEdit:array of TEdit;
      i:integer;
  begin
    for i:=0 to 9 do begin
    arrEdit[i]:=TEdit.Create(self);
    arrEdit[i].Parent:=artikelliste;
    arrEdit[i].Top:=50;
    arrEdit[i].Left:=50;
    arrEdit[i].Width:=50;
    freeandNil(arredit[i]);
    end;
  end;


Aber Leider bekomme ich immernoch die gleiche Fehlermeldung. :?
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Sa 09.12.06 20:52 
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:
unit Unit11;

interface

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

type
  TArtikelliste = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  procedure element;
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
    arrEdit: arraya of TEdit;
  end;

var
  Artikelliste: TArtikelliste;

implementation
 
{$R *.dfm}

procedure TArtikelliste.element;
      i:integer;
  begin
    SetLength(arrEdit,10);
    for i:=0 to 9 do 
    begin
        arrEdit[i]:=TEdit.Create(self);
        arrEdit[i].Parent:=artikelliste;
        arrEdit[i].Top:=i*50; //sonst liegen ja alles übereinander?!?
        arrEdit[i].Left:=i*50; //hier genauso!!!
        arrEdit[i].Width:=50;
   end;
  
  end;
procedure TArtikelliste.Button1Click(Sender: TObject);
begin
  element;
end;

//OnDestroy der Form
FreeAndNil(arrEdit);
end.


Hab das mal was umgeändert... ;)

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
zuma
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 660
Erhaltene Danke: 21

Win XP, Win7, Win 8
D7 Enterprise, Delphi XE, Interbase (5 - XE)
BeitragVerfasst: Sa 09.12.06 21:00 
user profile icondelphistart hat folgendes geschrieben:
Habe das jetzt mal so umgestelt:

hab mal kommentiert:
user profile icondelphistart hat folgendes geschrieben:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
procedure TArtikelliste.element;
  var arrEdit:array of TEdit;
      i:integer;
  begin
    for i:=0 to 9 do begin
    arrEdit[i]:=TEdit.Create(self);   // ok
    arrEdit[i].Parent:=artikelliste;  // ok
    arrEdit[i].Top:=50;  
    arrEdit[i].Left:=50;
    arrEdit[i].Width:=50;             // die 3 ok, aber alle 9 selbe Pos?
    freeandNil(arredit[i]);           // hier wird das soeben erzeugte wieder zerstört
    end;
  end;


Aber Leider bekomme ich immernoch die gleiche Fehlermeldung. :?


versuch mal dies :
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
procedure TArtikelliste.element;
  var i:integer;
  begin
    for i:=0 to 9 do begin
     FarrEdit[i]:=TEdit.Create(self);   
     FarrEdit[i].Parent:=artikelliste;  
     FarrEdit[i].Top:=50;  
     FarrEdit[i].Left:=50;
     FarrEdit[i].Width:=50;             
    end;
  end;


in das onclose der Form schreibst Du:

for i := Low(FarrEdit) to High(FarrEdit) do
freeandNil(arredit[i]);

in den pulic-bereich der type-anweisung (type TArtikelliste..) schreibst du

FarrEdit : array of TEdit;

hoffe, das hilft

_________________
Ich habe nichts gegen Fremde. Aber diese Fremden sind nicht von hier! (Methusalix)
Warum sich Sorgen ums Leben machen? Keiner überlebts!
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Sa 09.12.06 21:03 
Wo hat er denn die Länge des Arrays gesetzt? :lupe:

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
delphistart Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 119



BeitragVerfasst: Sa 09.12.06 21:06 
Also ich habe jetzt alles probiert, aber nix ging.
Nun hab ich einfach deinen ganzen geänderten Quelltext in Delphi kopiert und meinen damit ersetzt. Nun gehts und ich denke ich habe auch verstanden wie!

Also THX!!! :D
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Sa 09.12.06 21:09 
Und wieder jemanden glücklich gemacht ;)

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
zuma
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 660
Erhaltene Danke: 21

Win XP, Win7, Win 8
D7 Enterprise, Delphi XE, Interbase (5 - XE)
BeitragVerfasst: Sa 09.12.06 21:13 
ups, vergessen, die 'harte Methode'
für's onFormcreate zu schreiben :)
(is Sa. abend, das Glas bereits 2te mal leer ... ;) )
würd ich an seiner stelle da machen,
wo er die Anzahl der Datensätze aus
der Datei gelesen hat und diesen Integer
dann auch als Parameter der procedure
element für die Schleifenbedingung mitgeben.

_________________
Ich habe nichts gegen Fremde. Aber diese Fremden sind nicht von hier! (Methusalix)
Warum sich Sorgen ums Leben machen? Keiner überlebts!