Autor Beitrag
Chilloutfuchs
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 26

Win XP Pro
Delphi 7
BeitragVerfasst: Mi 20.08.03 23:58 
Ich habe folgendes Problem: Wenn ich von meinem MDI_Form den Befehl BringToFront an ein MDI_Child sende, stürzt das Programm mit einer Exseption ab!

Ich möchte folgendes realiesieren:
Ich habe zwei Buttons auf dem Hauptfenster (MDI_Form), und zwei MDI_Childs. Klickt der User auf Button 1 wird das erste MDI_Child geöffnet. Auf dem zweiten Button soll das zweite MDI_Child geöffnet werden. Wenn der User jetzt wieder auf den ersten Button klickt, und das zweite MDI Fenster ist noch offen, soll das erste Fenster in den Vordergrund gebracht werden.

Das passiert aber nicht, sondern ich bekomme halt die Exception.

Ich hoffe mir kann jemand sagen was ich falsch gemacht habe!

Mein Code sieht so aus:


Button 1 auf dem Hauptfenster (MDI_Form):

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure TMainForm.SpeedButton1Click(Sender: TObject);
begin
  IF Shipwindowstatus = 0 THEN
  BEGIN
    Ship_Window.TShipwindow_form.Create(Application);
    Shipwindowstatus := 1;
  END
  ELSE
  BEGIN
    IF Villagewindowstatus = 1 THEN ShipWindow_Form.BringToFront;
  END;
end;



Button 2 auf dem Hauptfenster (MDI_Form):

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure TMainForm.SpeedButton2Click(Sender: TObject);
begin
  IF Villagewindowstatus = 0 THEN
  BEGIN
    towns_window.TTownWindow_Form.Create(Application);
    Villagewindowstatus := 1;
  END
  ELSE
  BEGIN
    IF Shipwindowstatus = 1 THEN TownWindow_Form.BringToFront;
  END;
end;



On_Close Event vom ersten MDI_Child:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
procedure TShipwindow_form.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action := caFree;
  Main.Shipwindowstatus := 0;
end;



On_Close Event vom zweiten MDI_Child:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
procedure TTownWindow_Form.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action := caFree;
  Main.Villagewindowstatus := 0;
end;


Die MDI_Childs werden nicht beim Programmstart erzeugt! Der Code:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
program Test_Projekt;

uses
  Forms,
  MAIN in 'MAIN.PAS' {MainForm},
  ship_window in 'ship_window.pas' {Shipwindow_form},
  towns_window in 'towns_window.pas' {TownWindow_Form},

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TMainForm, MainForm);
 // Application.CreateForm(TShipWindow_Form, ShipWindow_Form);
 // Application.CreateForm(TTownWindow_Form, TownWindow_Form);
  Application.Run;
end.



Vielen Dank für eure Hilfe!

Moderiert von user profile iconTino: Delphi-Tags hinzugefügt.

_________________
Leben und leben lassen....
MSCH
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1448
Erhaltene Danke: 3

W7 64
XE2, SQL, DevExpress, DevArt, Oracle, SQLServer
BeitragVerfasst: Fr 22.08.03 14:44 
Schau dir mal deinen Code An:

ShipWindow_Form.BringToFront aber du verwendest
Ship_Window.TShipwindow_form.Create(Application)

Du referenziert Zeiger die in Leere Zeigen, da du deren Referenz nicht setzt.

als erstes deklariere mal deine Childfenster in der Hauptform:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
type
  ...
  private
    FirstChild: TShipwindow_form;
    SecondChild: TTownWindow_Form;
  ...

in den Aktionen musst du mit:
ausblenden Quelltext
1:
  FirstChild:= TShipWindow_Form.Create(Self);					

bzw.
ausblenden Quelltext
1:
  SecondChild:= TTownWindow_Form.Create(Self);					

die Childs erzeugen.

zum Anzeigen verwende:
FirstChild.BringToFront;

P.S. es ist übrigens kein guter Stil, über klassen hinwegzugreifen bzw. noch die Unit zu referenzieren.
Zitat:
Main.Villagewindowstatus := 0;
towns_window.TTownWindow_Form.Create(Application);

grez
msch
Chilloutfuchs Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 26

Win XP Pro
Delphi 7
BeitragVerfasst: Sa 23.08.03 16:31 
Juhu, klappt perfekt! Vielen Dank! Das ist mein erstes MDI Projekt, deswegen hatte ich keine Ahnung wie es wirklich geht.

Zum Programmierstil: Wenn du mir sagst, wie ich Globale Variablen deklariere, auf die ich von verschiedenen Units drauf zugreifen kann, werde ich das sofort ändern. Mir gefällt das auch absolute nicht, war auch eher eine Notlösung, weil ich es nicht anders hinbekommen habe. :D

_________________
Leben und leben lassen....
MSCH
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1448
Erhaltene Danke: 3

W7 64
XE2, SQL, DevExpress, DevArt, Oracle, SQLServer
BeitragVerfasst: Sa 23.08.03 17:38 
Auf globale Variablen zurückzugreifen ist wirklich eine Notlösung. Versuch es, darauf zu verzichen indem:
- du Properties deiner Klassen verwendest
- Messages verschickst

z.b. Um deiner MDI-Hauptanwendung zu sagen, dass ein Child geschlossen wurde:

die globale Constante ( in einer separaten Unit):
ausblenden Delphi-Quelltext
1:
2:
const
  WM_CloseMyFirstMdiChild = wm_user + 1;


Hauptanwendung ...
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
  private
    isFirstMdiChildActive: Boolean;
  public
    procedure WMCloseMyFirstMdiChild(var Msg:TMessage);Message wm_CloseMyFirstMdiChild;
...
procedure TMyHauptAnwendung.CreateMDIFirstChild;
begin
  MyChild:= MDIChild.Create(Self,Self.Handle);
  isFirstMdiChildActive:= True;
end;

procedure TMyHauptAnwendung.WMCloseMyFirstMdiChild;
begin
  isFirstMdiChildActive:= False;
end;


MDIChild
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
  ....
  private
    MyParentWindow: HWnd;
  public
  Constructor Create(AOwner:TComponent;AHandle:Hwnd);

constructor TMyChild.Create;
begin
  Inherited Create(AOwner);
  MyParentWIndow:= AHandle;
end;

in der MDI.Close oder Onclose Event einfach
..
  PostMessage(MyParentWindow,wm_CloseMyFirstMdiChild,0,0);
...


grez
msch
Chilloutfuchs Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 26

Win XP Pro
Delphi 7
BeitragVerfasst: Sa 23.08.03 17:50 
Aha, ist ja ein ganz andere stil, aber okay danke, ich werde es mal ausprobieren...

_________________
Leben und leben lassen....