1
votes

I am working on a project and so far it works fine. I have 3 forms and there will be more then, but now to the problem:

When the program starts Form1 will be shown. On a button I call this to open a new form:

Form2 frm = new Form2();
frm.ShowDialog();

Well, this works fine. Form2 will be shown in front of Form1 and if I close it Form1 is again the main form.

In Form2, I want to do the same, now, but Form2 should be closed and Form3 should be in the front. Well, this is not very difficult, but when I call it like that...

Form3 frm = new Form3();
frm.ShowDialog();
this.Close();

... Form2 will not close, because of ShowDialog. What can I do? I want that Form3 is in the front, Form2 is closed and Form1 is still there, but I cannot click on it, as Form3 should be called with ShowDialog.

At the moment Form3 is shown over Form2, but I want that it is shown over Form1. What can I do to solve this?

Of course the forms have other names, it is just to make it easier to understand.

Here you can see two pictures for understanding it better:

This is how it looks: http://www.directupload.net/file/d/3508/xmy99iwq_png.htm

Form2 is visible, but I want it to be closed.

Like here:

http://www.directupload.net/file/d/3508/qjvy42wq_png.htm

But I want that Form3 is in the front and you cannot click on Form1, like I called Form3 with ShowDialog in Form1.

1
cant you just switch the order to close form2 before calling frm.ShowDialog();Jonesopolis
I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not".John Saunders
@Jonesy Logically not, because when I close Form2 before, I cannot show the new form and I also do not want to hide it.Dominic B.
do you always want form3 to show when form2 is complete? Or is there a scenario where form2 should close, but form3 should not be shownJonesopolis
It should always show it, as a solid order.Dominic B.

1 Answers

1
votes

Don't have Form1 call Form2.ShowDialog. Have it call some other methods instead. Add a new method to Form2 that does what Form2 should do when shown, such as this:

public class Form2 : Form
{
    public void Display()
    {
        ShowDialog();
        Form3 dialog = new Form3();
        //TODO pass in parameters
        dialog.ShowDialog();
    }
}

Another option would be to have some entirely separate class handle this, possibly Form1, or possibly some other type that is essentially a public "wrapper" to Form2 and Form3, so that users of those forms (i.e. Form1) only ever use this wrapper class that would look something like this:

public class Foo //TODO give better name
{
    public void ShowPopups()
    {
        Form2 firstDialog = new Form2();
        firstDialog.ShowDialog();
        Form3 secondDialog = new Form3();
        //TODO pass in parameters
        secondDialog.ShowDialog();
    }
}