Search code examples
c#formswinformshideshowdialog

C# All forms but Owner sent to back when I hide a ShowDialog form


A form is shown using ShowDialog(this). User closes it and instead of disposing it I want to preserve changes and keep it ready till next open call:

        private FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }

But hiding the dialog results in sending to back all other opened forms but the Owner. Using Show(this) instead of ShowDialog() fixes it but I need to keep it opening as a dialog.


Solution

  • You don't need to hide it. Keep a reference to the form and show it again when you need:

    YourDialogForm f;
    private void button1_Click(object sender, EventArgs e)
    {
        if(f==null)
            f = new YourDialogForm();
    
        f.ShowDialog();
    }
    

    More information:


    Also if it is a setting form, you can add/use the Settings.settings file in your project and load and save settings. This way you don't need to rely on the form state. This way you can bind controls properties to settings properties and then call Properties.Settings.Default.Save(); when closing form.