Autor Beitrag
AceTheFace
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39



BeitragVerfasst: Mo 16.12.02 12:33 
Hallo,

ich möchte den Inhalt einer Datenbank in eine zur Laufzeit generierte Listbox werfen und diese dann in eine Listbox auf meiner Oberfläche kopieren.
Auslesen und erstellen mache ich so:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
function TStandortVerwalter.DB_einlesen:TListbox; 
var StandortListe:TListbox;
    aktStandort:TStandort;
begin
StandortListe:=TListbox.create(nil);
aktStandort:=TStandort.create;
QStandort.SQL.clear;
QStandort.SQL.Add('Select Bezeichnung from Standort');
QStandort.open;
QStandort.First;
while not QStandort.EOF do begin
        aktStandort.Bezeichnung:=TStringField(QStandort.fieldbyname('Bezeichnung')).asString;
        aktStandort.ID:=TStringField(QStandort.fieldbyname('ID')).asInteger;
        StandortListe.Items.AddObject(aktStandort.Bezeichnung,TStandort(aktStandort));
        QStandort.next;
        end;
result:=StandortListe;

end;


Die Befehle in der Gui sehen so aus:

ausblenden Quelltext
1:
2:
3:
4:
procedure TForm1.Button2Click(Sender: TObject);
begin
Listbox1.Items:=verwalter.DB_einlesen.Items;
end;


Doch wenn ich auf den Button drücke bekomme ich immer:
"Control '' has no parent window"

Liegt das daran, dass ich Listbox.create(nil) gemacht habe oder so?!

Gruss und danke,

Ace
smiegel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 992
Erhaltene Danke: 1

WIN 7
D7 Prof., C#, RAD XE Prof.
BeitragVerfasst: Mo 16.12.02 12:55 
Hallo,

warum mit Kanonen auf Spatzen schiessen?

Wenn Du Dir einmal die Deklaration der Listbox.Items genauer angeschaut hättest, würdest Du feststellen, dass die Items in einer Stringliste verwaltet werden.

Folgend eine Modifizierung Deines Codes:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
function TStandortVerwalter.DB_einlesen(aItems:TStrings):Integer; 
var aktStandort:TStandort; // ist das ein Record oder ein Objekt?
begin 
  Result:=0;
  if (aItems=nil) then Exit;
  aItems.Clear;
  with QStandort do
  begin
    SQL.clear; 
    SQL.Add('Select Bezeichnung, ID from Standort'); 
    Open; 
    Result:=RecordCount;
    //QStandort.First;  --> überflüssig, da nach Open immer auf dem 1. DS
    while not EOF do begin 
      // FillChar(aktStandort, SizeOf(TStandort), #0); --> wenn Record
      aktStandort.Bezeichnung:=Fieldbyname('Bezeichnung').AsString; 
      aktStandort.ID:=Fieldbyname('ID')).AsInteger; 
      aItems.AddObject(aktStandort.Bezeichnung, TStandort(aktStandort)); 
      Next; 
    end; // while
  end; // with
end; // TStandortVerwalter.DB_einlesen

Aufgerufen wird das ganze dann so:
ausblenden Quelltext
1:
2:
3:
4:
procedure TForm1.Button2Click(Sender: TObject); 
begin 
  Verwalter.DB_einlesen(Listbox1.Items); 
end;

_________________
Gruß Smiegel
Ich weiß, daß ich nichts weiß, aber ich weiß mehr als die, die nicht wissen, daß sie nichts wissen. (Sokrates)


Zuletzt bearbeitet von smiegel am Mo 16.12.02 12:59, insgesamt 2-mal bearbeitet
AceTheFace Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39



BeitragVerfasst: Mo 16.12.02 12:55 
Also ich bin ein kleines Stückchen weiter gekommen. Ich muss wohl irgendwie über StandortListe.createParent(aParent) das "Elternfenster" festlegen. Allerdings steht das teil ja nur in einer unit, die keinerlei grafische sachen hat, kann also nicht einfach form1 oder so angeben.
Zum Testen benutze ich aber ja eine andere unit, wenn ich allerdings über StandortListe.createParent(TestForm.Form1) diese Form als Elternfenster angeben will, sagt er mir, dass er TestForm nicht kennt obwohl testform ja diese unit in der uses klausel benutzt.....

Probleme über Probleme...bitte um Hilfe :)

Gruss,

Ace
AceTheFace Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39



BeitragVerfasst: Mo 16.12.02 12:59 
Standort ist ein Objekt....

ausblenden Quelltext
1:
2:
3:
4:
type TStandort=class
      Bezeichnung:String;
      ID:Integer;
      end;


Gruss,

Ace
smiegel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 992
Erhaltene Danke: 1

WIN 7
D7 Prof., C#, RAD XE Prof.
BeitragVerfasst: Mo 16.12.02 13:04 
Hallo, dann musst Du das Objekt auch erzeugen, bevor Du es mit AddObject in die Liste kopierts.

In etwa so:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
    while not EOF do begin 
      aktStandort:=TStandort.Create;
      aktStandort.Bezeichnung:=Fieldbyname('Bezeichnung').AsString; 
      aktStandort.ID:=Fieldbyname('ID')).AsInteger; 
      aItems.AddObject(aktStandort.Bezeichnung, aktStandort); 
      Next; 
    end; // while

_________________
Gruß Smiegel
Ich weiß, daß ich nichts weiß, aber ich weiß mehr als die, die nicht wissen, daß sie nichts wissen. (Sokrates)
AceTheFace Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39



BeitragVerfasst: Mo 16.12.02 18:34 
DANKE :!:

Hast mir sehr geholfen :)
Gruss,

Ace