Autor Beitrag
Azzidodabass
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 32



BeitragVerfasst: Di 28.10.08 21:49 
Hallo an alle,

habe folgendes Problem:

Möchte in einem Formular ein weiteres Formular öffnen, wenn man auf den button neues Projekt klickt


ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
private void neuesProjektToolStripMenuItem_Click(object sender, EventArgs e)
        {

            Form2 f=new Form2();
            f.Show();
            

            
        }



allerdings möchte ich nicht, dass sich das fenster wieder und wieder öffnet, wenn man erneut auf "neues Projekt" klickt.

Das Formular Form1 soll immer das Hauptformular sein, Form2 darf nicht hinter Form1 rutschen

Hoffe Ich hab mich verständlich ausgedrückt

Danke im Voraus

Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
Moderiert von user profile iconChristian S.: Topic aus C# - Die Sprache verschoben am Di 28.10.2008 um 20:51
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: Mi 29.10.08 10:14 
Hallo,

in diesem Fall darf das Hauptformular das neue Formular kennen, siehe meinen anderslautenden Hinweis in Formular abfragen.
Also geht es auf diesem Weg:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
//  Deklaration außerhalb des EventHandlers
Form2 secondForm = null;

//  beim Klick öffnen, ggf. vorher erzeugen
private void neuesProjektToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (secondForm == null) {
        secondForm = new Forms();
        //  hierher gehört das zusätzliche Ereignis (siehe unten)
        secondForm.SetClosedEvent(secondFormClosed);
    }
    secondForm.Show();
}

Achtung: Wenn Du das zweite Formular mit Close() vom Hauptformular aus schließt, musst Du secondForm = null setzen; sonst knallt es beim nächsten Aufruf eines neuen Projekts. Wenn Du das zweite Formular aus sich heraus schließt, muss das Hauptformular das erfahren; also musst Du ein Ereignis einrichten:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
//  im Hauptformular
private void secondFormClosed(object sender, FormClosedEventArgs e) {
    secondForm = null;
}

//  im zweiten Formular
public void SetClosedEvent(FormClosedEventHandler event) {
   this.FormClosed += event;
}

Für Einzelheiten bitte ich in der SDK-Doku/MSDN nachzulesen. Jürgen

PS. Bitte bemühe Dich um sprechende Bezeichner - nicht Form1, Form2 usw.
Azzidodabass Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 32



BeitragVerfasst: Mi 29.10.08 16:30 
Grüß dich,

klappt soweit alles gut, möchte in dem unterformular berechnungen durchführen, das fenster soll nur über das hauptformular geschlossen werden können.
Werde die controlbox von dem unterformular entfernen.



ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
private void neuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (f == null)
            {
                f = new Form2();
                f.MdiParent = this;
                f.Show();
            }
        }

        private void beendenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            f = null;
            Form2.Close();///// das klappt leider noch nicht
        }



leider kann ich mit dem beenden button das fenster noch nich schliessen, bekomme folgende fehlermeldung

Für das nicht statische Feld, die Methode oder die Eigenschaft "System.Windows.Forms.Form.Close()" ist ein Objektverweis erforderlich. C:\Users\Administration\AppData\Local\Temporary Projects\WindowsFormsApplication1


Danke für deine Unterstützung

Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: Mi 29.10.08 16:56 
Zitat:
Für das nicht statische Feld, die Methode oder die Eigenschaft "System.Windows.Forms.Form.Close()" ist ein Objektverweis erforderlich. C:\Users\Administration\AppData\Local\Temporary Projects\WindowsFormsApplication1

Es soll doch ein bestimmtes Formular geschlossen werden, nicht eine Klasse. Also musst Du dieses bestimmte Formular direkt verwenden; außerdem muss die Reihenfolge beachtet werden:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
private void beendenToolStripMenuItem_Click(object sender, EventArgs e)
{
    f.Close();
    f = null;
}

Gruß Jürgen