-1

I have very simple WinForms application, which consists of three forms: Form1, Form2 and Form3. Form1 is the main application window. By hitting a button in this window user opens Form2 as a modal dialog through a call to ShowDialog(this). Similarly, Form2 has another button, which opens Form3 through another call to ShowDialog(this). In each case this points to hosting from: Form1 and Form2 respectively.

It all works very fine until user hist OK button in Form3. At this moment both Form3 and Form2 modal windows close and user returns to Form1. Logically, user should return to Form2, from which he or she launched Form3. Why it happens? Thank you.

Form1 opens Form2 in the following code:

private void form1_ButtonClick(object sender, EventArgs e)
{
    Form2 settings = new Form2();
    DialogResult result = settings.ShowDialog(this);
    if (result == DialogResult.OK)
    { }
}

Then Form2 opens Form3 in this code:

private void form2_ButtonClick(object sender, EventArgs e)
{
    Form3 settings = new Form3();
    DialogResult result = settings.ShowDialog(this);
    if (result == DialogResult.OK)
    { }
}

Interestingly, debugger after exiting function form2_ButtonClick() immediately drops into form1_ButtonClick() as a next step. I inspected all calls of handlers and did not see any code associated with form closing. Even when I comment our all code of handlers forms still close in cascade.

I suspected that form closing is due to dialog result assigned to form buttons, so I disabled both and added explicit button handlers. Result remains the same. Also, I commented out all processing of dialog result in both functions. Still, closing Form3 magically closes also its parent Form2 and drops directly into Form1.

As advised, I ve also added explicit Form2.FormClosing() handler and put break-point there. Yes, this handler is called immediately on closing Form3 without any code suggesting this call.

Solution (partial)

Not being able to find reasons, I followed valuable advise by @Otterprinz and modified handler as follows.

private bool allowClose = true;
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
  if (!allowClose)
  {
    e.Cancel = true;
    allowClose = true;
  }
}

Before showing Form3 I assign allowClose = false. It works and prevents Form2 from abrupt closing. Why this situation happens at all I dont know. The forms are not complex at all. I ve carefully inspected their source code and did not see any wrong. These are not first and not even 100th forms I ve designed. Never seen this effect before. But, at least I ve found the solution due to your valuable advises. Thank you all for comments!

Boris Zinchenko
  • 2,142
  • 1
  • 24
  • 34
  • The 'ShowDialog()' method has a return value of 'DialogResult'. Do you do something with it? – RedHairy Jan 30 '18 at 10:25
  • it seems that Form2 is forced to close programmatically. So this is happened because of logic in the code. – Anton Semenov Jan 30 '18 at 10:28
  • I just wanted to ask, what the event handlers for the buttons look like. With just opening form2/form3, respectively closing form3 it works for me. – RedHairy Jan 30 '18 at 10:30
  • 2
    Without the real code example, I think there's nothing we can do but guess. – mrogal.ski Jan 30 '18 at 10:32
  • @RedHairy, Thank you for comments. Of course, I process DialogResult in my button handlers. But Form2 nowhere attempts to close itself, either in this handler or any other function. I m not sure what code I can share here, if not the whole project with these forms. – Boris Zinchenko Jan 30 '18 at 10:36
  • @Anton Semenov, thank you for your comment. – Boris Zinchenko Jan 30 '18 at 10:36
  • 1
    Put an eventhandler on Form2.FormClosing und put a breakpoint in there. If it's your code that does it, the callstack will show. – Otterprinz Jan 30 '18 at 10:56
  • 1
    @Otterprinz, thank you. Updated question text with your idea explored without any effect. – Boris Zinchenko Jan 30 '18 at 11:13
  • Verify the designer generated code - see where handlers are attached. You probably have two handlers attached for the same method or something like this. – BartoszKP Jan 30 '18 at 11:14
  • @BartoszKP, thank you. I will inspect now all designer code as text in a hope to find something. – Boris Zinchenko Jan 30 '18 at 11:16
  • 1
    @Otterprinz, I m tending to accept your solution and just put "e.Cancel = true;" into FormClosing() handler. The only problem is to distinguish where from went closing call. If applied universally, form wont be possible to close in principle! – Boris Zinchenko Jan 30 '18 at 11:18
  • 1
    @BorisZinchenko I'm a fan of finding the reasons, but i don't know your schedule so sometimes "it works better than before" is good enough. – Otterprinz Jan 30 '18 at 11:23
  • 1
    Please stop adding the "Updates". This makes the question completely unreadable. People in the future, looking for help will not be interested in the story of evolution of the post in the post itself (if they will - they can access it through the history link). Just post the relevant code that allows to reproduce the problem. – BartoszKP Jan 30 '18 at 11:39
  • https://stackoverflow.com/a/9792493 refer to this answer. it works to my case. – user15358028 Mar 09 '21 at 03:32

2 Answers2

0

Tested on a winform project and it is working as intended. Asker's code probably is not the same(equivalent) to the one he posted.

    // form1,form2,form3 are default forms
    // form1 button handler 
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 form = new Form2();
        DialogResult result = form.ShowDialog(this);
    }

    // form2 button handler
    private void button1_Click(object sender, EventArgs e)
    {
        Form3 form = new Form3();
        DialogResult result = form.ShowDialog(this);
    }

When closed, form3 returns focus and control to form2. Gif showing behaviour https://media.giphy.com/media/3ohs4lqu1NIy4i7hEA/giphy.gif

zenwaichi
  • 187
  • 1
  • 1
  • 10
0
Form2 settings = new Form2();
DialogResult result = settings.ShowDialog(this);
// ShowDialog will wait settings close