Frage

I have two forms, Form 2 is inheriting from Form 1.

What I need to do is that when I close both Form 1 and Form 2 another form which asks if user is sure to quit appears. Then if user clicks Yes, another form which asks if the user wants to save the game appears if and only if the form which the user closes is Form 2 and not Form 1 since for Form 1 there is no saving necessary.

This is what I managed to do:

// These are the Form 1 closing and closed event handlers:

private void GameForm_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = true;
    SureClose sc = new SureClose();
    sc.StartPosition = FormStartPosition.CenterScreen;
    sc.Show();
}

private void GameForm_FormClosed(object sender, FormClosedEventArgs e)
{
    MainMenu menu = new MainMenu();
    menu.Show();
}

Then in Sure Close: // Please note that Tournament is Form 2 inheriting from GameForm (Form 1)

 private void yesButton_Click(object sender, EventArgs e)
 {
        this.Hide();

        if (GameForm.ActiveForm is Tournament)
        {
            SaveGame sg = new SaveGame();
            sg.StartPosition = FormStartPosition.CenterScreen;
            sg.Show();
        } 
        else 
        {
            GameForm.ActiveForm.Close();
        }
    }

    private void noButton_Click(object sender, EventArgs e)
    {
        this.Hide();
    }

// This is the SaveGame Form:

 private void saveButton_Click(object sender, EventArgs e)
 {
     // Still to do saving!
 }

 private void dontSaveButton_Click(object sender, EventArgs e)
 {
     this.Hide();
     GameForm.ActiveForm.Close();
 }

The problem is that when in the yesButton event handler in SureClose Form I have GameForm.ActiveForm.Close(), this is going back to the GameForm Closing event handler therefore the SureClose dialog is appearing again.

I tried doing: if (e.CloseReason() == CloseReason.UserClosing) but obviously it doesn't work either because the reason of closing will always be the user :/

How can I solve this? Thanks a lot for any help !

War es hilfreich?

Lösung

Form1 :

private void GameForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if(SureClose())
    {
        SaveChanges();
    }
    else
    {
        e.Cancel = true; 
    }
}

private bool SureClose()
{
    using(SureClose sc = new SureClose())
    {
        sc.StartPosition = FormStartPosition.CenterScreen;
        DialogResult result = sc.ShowDialog();
        if(result == DialogResult.OK)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

protected virtual void SaveChanges()
{
}

Form2:

protected override void SaveChanges()
{
    using(SaveGame sg = new SaveGame())
    {
        sg.StartPosition = FormStartPosition.CenterScreen;
        DialogResult result = sg.ShowDialog();
        if(result == DialogResult.OK)
        {
            //saving code here
        }
    }
}

SureClose form and SaveGame form:

private void yesButton_Click(object sender, EventArgs e)
{
    this.DialogResult = DialogResult.OK;
}

private void noButton_Click(object sender, EventArgs e)
{
    this.DialogResult = DialogResult.Cancel;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top