如何检查Windows窗体是否已经存在,如果存在,则将其打开,如果不存在,则创建一个新窗体? [英] How to check if a windows form already exists, open it if it does, creates a new one if it doesn't?
本文介绍了如何检查Windows窗体是否已经存在,如果存在,则将其打开,如果不存在,则创建一个新窗体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我基本上想要做到这一点的东西
I basically want something that does this:
if (form.isOpen() == true)
form.ShowDialog();
else
form f = new form();
我有一个表单,显示添加到订单中的项目的列表,但是当我转到另一个页面并导航回以生成订单表单时,它将调用form f = new form();
,我相信它将重置该表单.关于如何可以克服的任何建议?
I have a form that displays a list of items added to an order, but when I go to another page and navigate back to make an order form it calls form f = new form();
, which I believe resets the form. Any suggestions of how this can be overcome?
推荐答案
您可以使用Application.OpenForms
属性检查打开的表单,如下所示:
You can check which forms are open using Application.OpenForms
property as follows:
if (Application.OpenForms.OfType<MyForm>().Any())
{
Application.OpenForms.OfType<MyForm>().First().BringToFront();
}
else
{
form f = new MyForm();
f.show();
}
这篇关于如何检查Windows窗体是否已经存在,如果存在,则将其打开,如果不存在,则创建一个新窗体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文