Richard
Richard

Reputation: 7433

How do I open a series of continuous forms one at a time?

Currently, I have the following structure: Form1 receives inputs. After inputs are recorded, you can click a button to proceed to Form2 that is dynamically created based on the inputs of Form1. Form2 gathers more inputs that will be used on Form3. In each form (i.e. Form1, Form2, Form3), you can go back to the previous Form in case you need to change your inputs. To do this, I will be closing the current Form and opening its Owner Form that I record manually in my code.

My current method of opening these multiple forms: hiding the current form when proceeding to the next form, closing the current form when backtracking to the previous form. A sample of my code:

private void next_Click(object sender, EventArgs e)
{
    List<int> intData = new List<int>();
    //Process intData's members

    this.Hide();
    SecondInputForm form2 = new SecondInputForm(intData);
    form2.Owner = this;
    form2.ShowDialog();
}

Where next is a Button in my Form1 that allows the user to proceed to the second form Form2. In my Form2, I have a similar construct to open Form3. However, I have another event listener FormClosing to open my previous form Form1 when Form2 is closed (i.e. backtracking to previous form):

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{            
    this.Owner.Show();
}

private void next_Click(object sender, EventArgs e)
{
    List<int> intData = new List<int>();
    //Process intData's members

    EquationInput form3 = new EquationInput(intData);
    form3.Show();
    form3.Owner = this;            
    this.Hide();
}

Now the problem occurs here. When I Hide() Form2, it registers as a FormClosing event and somehow closes Form2 and as a result, closing its 'child' form Form3. It then opens Form1.

My desired result is: it hides Form2, but does not close it, and proceeds with gathering inputs from user in Form3.

What is the better approach to opening multiple forms or how do I fix the current problem if Hide()-ing form is indeed the best approach? I feel like using Hide() to mimic a series of continuous forms is not really 'intuitive', one might say.

Upvotes: 0

Views: 57

Answers (1)

nalnpir
nalnpir

Reputation: 1197

Easy way to handle the problem of you are having is to set the cancel property to true from the FormClosing like this:

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
    this.Hide();     
    this.Owner.Show();
    e.Cancel = true;
}

but bear in mind that you should set some sort of condition to close it, else it will never be closed.

Upvotes: 1

Related Questions