Question

My Form1 opens a new form when "Show Images" is clicked. When the MW3Images comes up, it closes whether I click on "Next", "Previous" or "Close". I have set the "cancel button" option to use the "Close" button, but changing it makes no difference. Here is my source code for the "Show Images" button:

   private void showImages_Click(object sender, EventArgs e)
    {
        MW3Images MW3Images = new MW3Images();
        MW3Images.ShowDialog();
        if (MW3Images.DialogResult == System.Windows.Forms.DialogResult.Cancel)
            MW3Images.Close();
    }

And for the MW3Images form:

   public MW3Images()
    {
        InitializeComponent();
        pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        Controls.Add(pictureBox1);

        pictureBox1.Image = Properties.Resources.boxart;

        next.DialogResult = System.Windows.Forms.DialogResult.OK;
        previous.DialogResult = System.Windows.Forms.DialogResult.OK;
        imagesQuit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    { }
    private void Images_Load(object sender, EventArgs e)
    { }
    private void imagesQuit_Click(object sender, EventArgs e)
    { }
    private void next_Click(object sender, EventArgs e)
    { }
    private void previous_Click(object sender, EventArgs e)
    { }
}

I had several if statements under "Next" and "Previous", but their presence did not affect the form closing. Any help would be much appreciated. Thanks!

Était-ce utile?

La solution

This is the correct behavior when you call .ShowDialog(). See the documentation

When this method is called, the code following it is not executed until after the dialog box is closed. The dialog box can be assigned one of the values of the DialogResult enumeration by assigning it to the DialogResult property of a Button on the form or by setting the DialogResult property of the form in code. This value is then returned by this method.

and

Unlike non-modal forms, the Close method is not called by the .NET Framework when the user clicks the close form button of a dialog box or sets the value of the DialogResult property. Instead the form is hidden and can be shown again without creating a new instance of the dialog box. Because a form displayed as a dialog box is hidden instead of closed, you must call the Dispose method of the form when the form is no longer needed by your application.

In other words, as soon as you set a DialogResult, which happens when any button on the form which has such a result is clicked, then the form is hidden and the calling form continues executing its code.

You should either use .Show() instead of .ShowDialog(), or not use the DialogResult property of the buttons on your form. Instead, if you double-click them in the designer, you'll get an _Click() event that you can put your code in. Inside those functions is where you'd put the code that loaded up the next image - but there's no automatic way to do that. You'd have to write that yourself.

Autres conseils

Try this, set an owner for the Dialog:

MW3Images.ShowDialog(this);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top