简体   繁体   中英

Dialog in a Dialog window - Parent dialog closes with child dialog

In my main form I am opening a new form (lets call it the parent form) using ShowDialog(). In the parent form I have a button which loads an OpenFileDialog, when I load the image and click Open button, OpenFileDialog closes but it also closes the parent window and I dont want this to happen.

Main form code:

    // ADD GRAPHICS BUTTON
    private void bAddGraphics_Click(object sender, EventArgs e)
    {
        NewGraphics newGraphics = new NewGraphics();
        newGraphics.ShowDialog();

        if (newGraphics.DialogResult == DialogResult.OK)
        { 
            Core.Graphics tempGraphicsObject = new Core.Graphics();
            tempGraphicsObject.name = newGraphics.name;
            tempGraphicsObject.background = newGraphics.bgImage;
            core.NewGraphics(tempGraphicsObject);
            generateSingleGraphicsControl(core.project.graphics[core.project.graphics.Count-1].id, core.project.graphics[core.project.graphics.Count-1].name);
        }
        newGraphics.Dispose();
    }

Parent form(dialog)

        OpenFileDialog openFileDialog = new OpenFileDialog();
        DialogResult result = openFileDialog.ShowDialog();

        if (result == DialogResult.OK)
        {
            tbBackground.Text = openFileDialog.FileName;
            bgImage = Image.FromFile(tbBackground.Text);
        }
        openFileDialog.Dispose();

Is it because I use DialogResult twice or maybe because I call ShowDialog() in a dialog window ? If I won't use ShowDialog() on a parent but just Show() it works fine but then I can't use DialogResult property. Is there a way around it or you just can't use ShowDialog() twice ?

If the "parent" Form is closing too, then you're either calling Close() or setting the DialogResult property on the Form (which will also close it).

From MSDN , regarding the DialogResult property:

If the form is displayed as a dialog box, setting this property with a value from the DialogResult enumeration sets the value of the dialog box result for the form, hides the modal dialog box, and returns control to the calling form.

I don't see you doing either one of those in the code you posted, but check for a line like one of these in your "parent" Form:

DialogResult = DialogResult.OK;

this.DialogResult = DialogResult.Cancel;

Close();

this.Close();

I know I'm digging up an old post, but I just ran into this and found another situation that can cause this. It's common to copy/paste form controls and reuse them for other purposes. Make sure that if you copied your form's OK or Cancel button for the Browse File button that you remove the DialogResult value. I had done this and forgot to remove the DialogResult.OK from my folder browser button. Apparently, the DialogResult assignment will happen after the click event making it appear that the dialog closed its parent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM