开发者

how to open new window on form close?

how to open form 1 on close butt开发者_如何学Con click X in form 2 i tried this but it is not working:

private void supplierShow_FormClosing(object sender, EventArgs e)
{
    new suppliersList().Show();
}

Thank you.


Not fully sure without knowing more your code but to me it looks like you are declaring a local variable (new suppliersList of base class Form probably) and showing the form from the Closing event of another form.

Your Form2 object is probably going to be deleted/disposed/garbage collected soon and at that point I am not completely sure that the form declared within that scope would still have a nice life.

If Form2 is the application form, the application will actually terminate when you close it.

in general I think that this kind of form switching is best done and controlled from the main method, the same place where you probably have Application.Run(new Form2()); because in there you have full control of the application flow and MessageLoops...


I have to solutions:

First:

You may have a unique reference to your suppliersList in Program class:

public static suppliersList SuppList = new suppliersList();

you can now hide it if you want:

Program.SuppList.Hide();

you can show it anytime, too:

Program.SuppList.Show();

Be carefull! Don't Dispose SuppList. If you did, assign to it a new object new suppliersList();. Please note that this solution would success only if form1 is not the main form (i.e. Application.Run(new form1()); )

Second:

You can start form2 as a child of window of form1:

new suppliersList().Show(this);

because, as in the other answer, the application loops are on form1. If form1 is closed, then entire application will be closed too. However, starting a child window of the main window will prevent application close.

If the answer is useful for you, please mark it as your best answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜