Autor Beitrag
Cyron
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16

Win XP home SP2
C# (VS 2005 professional am DLR, VS2005 Academic Edition zu Hause)
BeitragVerfasst: Do 03.08.06 21:20 
Hallo,
gerade hat mich mein RSS-Reader über ein neues Snippet informiert:
www.dotnet-snippets....pet_detail.aspx?=182

Muß das so sein?
Ich hab's bislang immer so gemacht:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
private void ExitButton_Click(object sender, EventArgs e)
      {
         Dispose();
         Application.Exit();
      }

Ist das falsch?
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Fr 04.08.06 10:11 
Was hat der eine Code mit dem anderen zu tun :gruebel: ?
Der Snippet-Code zeigt einen Sicherheitsdialog an, um das Schließen eines Fensters vom User bestätigen zu lassen, und dein Code schließt alle Fenster und schlussendlich die Anwendung; das sind doch zwei Paar Schuhe.
Dein Code mit Application.Exit() ist soweit richtig[meta]Jedenfalls unter .Net 2.0, siehe SDK[/meta], den Dispose()-Aufruf solltest du aber entfernen. Ob du nun deinen Fenstern noch diese Sicherheitsabfrage hinzufügst, ist deine Entscheidung und hängt vom Typ der Anwendung ab.
Cyron Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16

Win XP home SP2
C# (VS 2005 professional am DLR, VS2005 Academic Edition zu Hause)
BeitragVerfasst: Fr 04.08.06 12:36 
Achso, jetzt sehe ich das. Der Author hat den Button "Form1_FormClosing" genannt.
Warum sollte ich Dispose() rausnehmen?
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Fr 04.08.06 19:56 
user profile iconCyron hat folgendes geschrieben:
Achso, jetzt sehe ich das. Der Author hat den Button "Form1_FormClosing" genannt.
Da ist kein Button, das ist das FormClosing-Event der Form, wie diese Zeile des verlinkten Codes zeigt:
ausblenden C#-Quelltext
1:
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);					

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Cyron Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16

Win XP home SP2
C# (VS 2005 professional am DLR, VS2005 Academic Edition zu Hause)
BeitragVerfasst: Fr 04.08.06 21:57 
Muß man diesen Eventhandler selber schreiben oder bietet der Forms-Designer so etwas? Wenn ja, wie? :oops:
Cyron Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16

Win XP home SP2
C# (VS 2005 professional am DLR, VS2005 Academic Edition zu Hause)
BeitragVerfasst: Fr 04.08.06 22:04 
Hat sich erledigt, danke :)
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Sa 05.08.06 11:16 
user profile iconCyron hat folgendes geschrieben:
Warum sollte ich Dispose() rausnehmen?
Wenn Application.Exit() erfolgreich ist, werden alle Forms automatisch freigegeben, darum musst du dich also nicht kümmern. Sollte nun aber Exit() (oder Close()) durch einen Eventhandler wie im Snippet-Code aufgehalten werden, sodass die Anwendung normal weiterläuft, hast du ein Problem, da du das Fenster ja schon zerstört hast.
Cyron Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16

Win XP home SP2
C# (VS 2005 professional am DLR, VS2005 Academic Edition zu Hause)
BeitragVerfasst: Sa 05.08.06 12:54 
Alles klar, danke. :)