Search code examples
c#formswinformsvisual-studioshowdialog

Windows form application sequential ShowDialog()s


well I have a funny problem with closing dialog forms. here is the problem:

I run application and open second form (through menu strip) as showdialog(); and then from second form open third form. When I open third form by button 1 and then close it everything is alright, but when I open the third form by button 2 and then close it, third form will be closed and then it closes the second form also. !!! In second form when I show a messageBox and close it also the second form will be closed.

here is my codes:

open second form from first form codes:

private void settingsToolMenu_Click(object sender, EventArgs e)
    {
        settingsForm s1 = new settingsForm(this);
        s1.ShowDialog();
    }

open third form from second by button 1 form codes:

private void addReportButton_Click(object sender, EventArgs e)
    {
        addReport a1 = new addReport(this);
        a1.ShowDialog();
    }

open third form from second by button 2 form codes:

private void editReportButton_Click(object sender, EventArgs e)
    {
        addReport a2 = new addReport(this);
        a2.ShowDialog();
    }

as you see there is no differences between button 1 and button 2

here is a video from application running.


Solution

  • in second form formClosing() event i wrote these codes:

    private void settingsForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        if(e.CloseReason != CloseReason.UserClosing)
        {
            e.Cancel = true;
        }
    }
    

    and nothing can close the second form except user!