-1

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.

rufatZZ
  • 743
  • 2
  • 11
  • 27

4 Answers4

0

Not sure what's happening out there, but there should be .Show() method, which runs a window in a different way including closing strategy. Try it out.

52hertz
  • 342
  • 4
  • 16
0

Try This

Instead of

addReport a2 = new addReport(this);
a2.ShowDialog();

Use

addReport a2 = new addReport();
a2.ShowDialog(this);

Then on click of Exit / Close button of dialog window

private void BtnExit_Click(object sender, EventArgs e)
    {
        this.Dispose();
    }

Hope this will solve your issue.

maddy23285
  • 738
  • 6
  • 16
0

I used this code and it worked. I have 3 forms, the first form is opened when running the app, the second form is opened with a button (can be menustrip, doesn't matter), then the third is opened like that too, after closing the third form the second form remains open.

FormN fm = new FormN();
fm.ShowDialog();

Use that piece of code in every method that is called from clicking on a button and it should work fine. Just change the "FormN" for whatever your forms are named. Also, if you need to pass any form's attributes into the next form you can do this:

Code at first form:

    public string mytext; //Variable I want to use later, in Form2.
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        mytext = tb1.Text;
        Form2 fm = new Form2(this);
        fm.ShowDialog();
    }

Notice how I save "tb1"'s (TextBox1) value in a variable before calling "fm.ShowDialog();", so I can use the TextBox1 value later inside the Form2.

Code at second form, having main form's variables (such as "mytext" value).

    Form1 mfm;
    public Form2(Form1 mainfm)
    {
        InitializeComponent();
        mfm = mainfm;
    }
    public void button2_Click(object sender, EventArgs e)
    {
        //In this method I use the variable "mytext" wich is a Form1 attribute.
        //You can see how I declare it in the first form's code (see above).
        textBox1.Text = mfm.mytext;
    }

With this you have created an object of your main form ("Form1 mfm;") with all the variables it contained before calling the second form, which can be used for the third form too.

ccolin
  • 304
  • 2
  • 7
0

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!

Komal12
  • 3,340
  • 4
  • 16
  • 25