4

I have been dealing with strange problem. I am using KryptonForm in a project. I have a form (say form1) and I need to open another form on a button click from this form. Here is the code:

void btn_click(object sender, EventArgs e)
{
    Visible = false;
    ShowInTaskbar = false;

    var f = new Form2();
    f.ShowDialog();

    Visible = true;
    ShowInTaskbar = true;
}

The problem is that when the Form2 closes it closes the Form1 also. I have tried setting DialogResult = DialogResult.None from Form2 but of no avail. Please help me.

I am always using this technique and this thing has never happened.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
  • Have you stepped through this in the debugger to see what is really going on? There could be some other code being executed which you haven't included here. – Darbio Dec 05 '10 at 12:32
  • What happens if you comment-out the 2 first lines in this method? Does Form1 still get closed if you avoid making it invisible and hiding its TaskBar button? – Ran Dec 05 '10 at 12:33
  • Is the parent form (`form1`) actually *closed*, or does it only stay *invisible*? – stakx - no longer contributing Dec 05 '10 at 12:47
  • @JD I have debugged it, @Ran haven't commented the lines that make `form1` invisible. Yes @stakx `form1` and even the form that opened it is closed :( – TheVillageIdiot Dec 05 '10 at 15:50

3 Answers3

7

Yes, this code is troublesome. It goes wrong when the user closes the dialog. Windows must then find another window to give the focus to. There isn't any left in your app, your main window is invisible. It then picks a window of another app. Odds are good, for example, that this will be a window inside Visual Studio. A big one. Your main form now disappears behind it.

You need to make sure that your main window is visible again before the dialog closes. You can do so by subscribing to the dialog's FormClosing event handler. For example:

    private void button1_Click(object sender, EventArgs e) {
        using (var dlg = new Form2()) {
            dlg.StartPosition = FormStartPosition.Manual;
            dlg.Location = this.Location;
            dlg.FormClosing += (s, ea) => this.Show();    // <=== Here
            this.Hide();
            if (dlg.ShowDialog() == DialogResult.OK) {
                // etc...
            }
        }
    }
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Yes @Hans I was also thinking on this line after asking question and going on for a walk. As we use this a lot in applications we build at work and only difference is that we don't hide the form from which we are showing another form as dialog. Let me check it, BRB :D – TheVillageIdiot Dec 05 '10 at 15:49
  • Thanks @Hans, using `Hide()` instead of `Visible = false;` worked. – TheVillageIdiot Dec 05 '10 at 16:07
  • Hmm, the Hide() method contains just one line of code: this.Visible = false; – Hans Passant Dec 05 '10 at 16:23
1

I know this is an old post, but I ran into this, and in my case the accepted answer (at the time I am writing this) is not helpful at all. The answer by @blind Skwirl led me to the culprit.

After 20 years of .Net programming (since it was introduced), I never noticed that BUTTONS have a "dialogresult" property. I always just set the forms "cancelbutton" and "acceptbutton" properties. What I found in my case was that (because I was doing a lot of copy-pasting of buttons), I had a bunch of buttons (not forms) that themselves had their "dialogresult" property set to "cancel", which meant that I would click a button on a dialog that would open another dialog, the "ok" button on the dialog had its result set to "cancel", and the button on the parent form ALSO had its result set to "cancel", so the dialog would close (with a result of cancel) and then the PARENT form would close with a result of cancel, confusing the heck out of me... so...

Just make sure all of your buttons have their dialogresult property set to NONE (or whatever the actual proper setting is that you want).

Bottom line, if a BUTTON (not the form) has its dialogresult property set to anything other than NONE, the form will close with that result when it is clicked (after any click event code has completed).

I hope that helps someone out there.

JesseChunn
  • 555
  • 3
  • 7
  • 1
    Hello JesseChunn, thank you for contributing to SO! Please try to keep your answer in an succinct, informative style (and use more paragraphs and less boldface font) so readers can easily identify the point you are making. – Bernhard Stadler Oct 31 '20 at 19:26
0

Bugged me for days!! Found this: https://bytes.com/topic/net/answers/769433-c-showdialog-inside-showdialog-closing-both-return

The result was being passed down and I dont know why. But if after the .ShowDialog() I just put this.DialogResult = DialogResult.None, it will fix it. This shouldnt happen in the first place, but this fixes it, so I am not too bothered.


You can also try changing the dialogResult on the button itself to "None" or deleting the this.Btn1.DialogResult... from the designer which worked for some people.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/543093ad-1860-4428-bae1-b0d4f112e04b/showdialog-closes-parent?forum=csharpgeneral

blind Skwirl
  • 321
  • 3
  • 6