Question

I have the following problem:

I open multiple modal forms in a stack (for example, form1 opens modal form form2 which in turn opens modal form form3, etc.). I would like to hide the entire stack.

I tried calling the Hide method or setting the Visible property on the parent, but this only hides the parent. I also tried hiding every form individually, but then I have to call ShowDialog on each of the forms which locks the thread in which I call the aforementioned method.

Is there be a way to set the modal dialogs so that they inherit the status of the parent and get hidden in a cascade just by setting the property on the first form?

I'm also open to other suggestions.

Was it helpful?

Solution

To re-show a form you hid by setting obj.Visible = false just set obj.Visible = true, not ShowDialog.

ShowDialog initiates a message loop, which will cause confusion since the dialog is already running a message loop.

OTHER TIPS

Since you're talking about modal dialogs, it would be the last one opened that would commence this action. Open every form as in the following example, and then Hide() that last one.

public partial class Form1 : Form
{
    Form2 frm2 = new Form2();

    public Form1()
    {
        InitializeComponent();
        frm2.VisibleChanged += frm2_VisibleChanged;
        Shown += Form1_Shown;
    }

    void Form1_Shown(object sender, EventArgs e)
    {
        frm2.ShowDialog();
    }

    void frm2_VisibleChanged(object sender, EventArgs e)
    {
        if (frm2.Visible == false) Hide();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top