0

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 !

Bernice
  • 2,552
  • 11
  • 42
  • 74
  • in the `GameForm_FormClosing` you could use a messagebox with yes / no instead. Then using a dialogresult to use different actions regarding the outcome. See [this link](http://stackoverflow.com/questions/3036829/messagebox-with-yes-no-and-dialogresult) for an example. – CuccoChaser Dec 07 '12 at 12:47
  • and why would a dialog box make a difference instead of another form? – Bernice Dec 07 '12 at 12:51
  • take a look at Dmitry's answer. It allows you to stay inside the same method and not having to go to another class and calling the previous method again (which is recursive ofcourse) and as seen with the virtual override in form1 it won't do anything, while in form2 it asks to save the game. – CuccoChaser Dec 07 '12 at 12:59

1 Answers1

3

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;
}
Dmitrii Dovgopolyi
  • 6,231
  • 2
  • 27
  • 44
  • Thanks for your help Dmitry! DialogResult result = sg.Show(); --- this is returning an error - cannot implicityly convert type void to DialogResult and this doesn't seem to exist in Form 2: saveButton_Click(null, null); – Bernice Dec 07 '12 at 13:01
  • Yes. My mistake. You should use ShowDialog() and aslo you should change your SaveGame and SureClose forms, so that they will return DialogResult.OK when right button is pressed – Dmitrii Dovgopolyi Dec 07 '12 at 13:04
  • But event handlers can't return anything, can they? :/ Sorry I'm new at this! – Bernice Dec 07 '12 at 13:10
  • When you set this.DialogResult in form, that was opened by ShowDialog() method, the form is closed and you will return to origin code – Dmitrii Dovgopolyi Dec 07 '12 at 13:13
  • `if(result == DialogResult.OK) { return true; } else { return false; }` can be simplyfied to `return result == DialogResult.OK`. – Olivier Jacot-Descombes Dec 07 '12 at 13:16
  • oh I see I understood how it works now and I did everything you told me to.. however saveButton_Click() in form 2 is still not existing! – Bernice Dec 07 '12 at 13:21
  • nothing that was a noob question ! I changed the event to public and it's working !! Thanks a lot Dmitry I wouldn't have thought of your method since I never knew DialogResult even exist!! – Bernice Dec 07 '12 at 13:24
  • This code means that you call saving code inside Form2 and use SaveGame Form only to ask user if he wants to save something. In this case you can change saveButton_Click(null, null); to your saving code. – Dmitrii Dovgopolyi Dec 07 '12 at 13:27