如何检查 Windows 窗体是否已存在,如果存在则打开它,如果不存在则创建一个新窗体? [英] How to check if a windows form already exists, open it if it does, creates a new one if it doesn't?

查看:31
本文介绍了如何检查 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆