Autor Beitrag
Danny87
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 688

Windows 10 Pro 64bit
Sprachen: HTML, PHP, JavaScript, Delphi || IDE: RAD Studio 10.1 Berlin Starter, WeBuilder
BeitragVerfasst: Di 14.02.06 13:54 
Hallo auch,
Ich erzeuge zu Laufzeit mit einem Buttonklick ein Formular.
Dieses Formular darf aber nur einmal erzeugt werden.
Ich weiss aber nicht wie ich abfragen kann, ob das Formular bereits existiert.
Ich hatte da schon eine Idee mit einer booleschen Variablen, aber das scheint mir
nicht sehr elegant.

Ich hab mir das ungefähr so vorgestellt:
ausblenden Delphi-Quelltext
1:
2:
if Form2.Existiert then
  Erzeuge Form2;



gruss daniel
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19314
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Di 14.02.06 15:17 
Wenn Form2 noch nicht existiert, dann kannst du auch auf kein darin enthaltenes Element, in diesem Fall deine boolsche Variable, zugreifen...
Du bekommst dann eine Zugriffsverletzung...

So gehts...
ausblenden Delphi-Quelltext
1:
2:
if Form2 = nil then
  Form2 := TForm2.Create(Self);

Solange Form2 noch nicht erzeugt wurde, ist die entsprechende Variable nil (der Pointer zeigt auf den Speicherbereich 0, wo nie zugegriffen werden darf).
Und genau das kannst du abfragen.
digi_c
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1905

W98, XP
D7 PE, Lazarus, WinAVR
BeitragVerfasst: Di 14.02.06 15:18 
Also ich finde es akzeptabel, ansonsten (da bin ich mir aber nicht sicher) müsstest du in einer Schleife prüfen ob unter den Formularen ein Formular ist, das von dieser Formularklasse abgeleitet wurde.
chrisw
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 439
Erhaltene Danke: 3

W2K
D7
BeitragVerfasst: Di 14.02.06 15:21 
Oder
ausblenden Delphi-Quelltext
1:
if assigned(Form2) then .....					

_________________
Man sollte keine Dummheit zweimal begehen, die Auswahl ist schließlich groß genug.
Danny87 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 688

Windows 10 Pro 64bit
Sprachen: HTML, PHP, JavaScript, Delphi || IDE: RAD Studio 10.1 Berlin Starter, WeBuilder
BeitragVerfasst: Di 14.02.06 15:51 
user profile iconjaenicke hat folgendes geschrieben:

So gehts...
ausblenden Delphi-Quelltext
1:
2:
if Form2 = nil then
  Form2 := TForm2.Create(Self);

Solange Form2 noch nicht erzeugt wurde, ist die entsprechende Variable nil (der Pointer zeigt auf den Speicherbereich 0, wo nie zugegriffen werden darf).
Und genau das kannst du abfragen.


Das wars was ich gesucht habe. Danke!
jojo-sp
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 317

Windows XP Prof, Vista Ultimate & Home Premium, Windows 7
Delphi 7 Enterprise, Delphi 2009
BeitragVerfasst: Di 14.02.06 16:05 
Vergiss nachher nicht das Freigeben des Forms. Entweder wenn du das Fenster verlässt, oder das ganze Programm beendest (kommt daruaf an, wo du dein Create machst).

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
If Assigned(EigenesForm) Then
Begin
  EigenesForm.Free;
  EigenesForm := nil;
end;

_________________
Ist der Ruf erst ruiniert, lebts sich gänzlich ungeniert...
Wilhelm Busch (1832 - 1908)
Danny87 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 688

Windows 10 Pro 64bit
Sprachen: HTML, PHP, JavaScript, Delphi || IDE: RAD Studio 10.1 Berlin Starter, WeBuilder
BeitragVerfasst: Di 14.02.06 16:07 
das create hab ich anders gemacht, weil ich mit

ausblenden Delphi-Quelltext
1:
Form2 := Form2.Create(self);					

eine Fehlermeldung bekomme.

habs so gemacht:

ausblenden Delphi-Quelltext
1:
Application.CreateForm(TForm2, Form2);					



//Edit:

Im OnClose von Form2
ausblenden Delphi-Quelltext
1:
2:
Destroy;
Form2 := nil;


Es handelt sich übrigens um ein MDI-Child
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Di 14.02.06 16:11 
Zitat:
das create hab ich anders gemacht, weil ich mit

ausblenden anzeigen Delphi-Quelltext
ausgeblendet markieren
1:

Form2 := Form2.Create(self);

eine Fehlermeldung bekomme.


es muß auch Form2 := TForm2.Create(self); heißen

_________________
Markus Kinzler.
Danny87 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 688

Windows 10 Pro 64bit
Sprachen: HTML, PHP, JavaScript, Delphi || IDE: RAD Studio 10.1 Berlin Starter, WeBuilder
BeitragVerfasst: Di 14.02.06 16:14 
Huch!
Verdammtes T ;-)

ja, jetzt gehts....
ich sollte vllt mal genauer hinschauen :-D

Jetzt funzt auch das Free;
jojo-sp
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 317

Windows XP Prof, Vista Ultimate & Home Premium, Windows 7
Delphi 7 Enterprise, Delphi 2009
BeitragVerfasst: Di 14.02.06 16:16 
Du hasst ja auch ein T vergessen:

ausblenden Delphi-Quelltext
1:
  Form2 := TForm2.Create(Self);					


aber die Funktion FormCreate kannte ich noch gar nicht ;-), aber wenns geht :gruebel:

_________________
Ist der Ruf erst ruiniert, lebts sich gänzlich ungeniert...
Wilhelm Busch (1832 - 1908)
digi_c
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1905

W98, XP
D7 PE, Lazarus, WinAVR
BeitragVerfasst: Di 14.02.06 16:48 
Ahh assigned, verdammt :oops: