C#如何检查表单是否已在紧凑框架中打开

发布于 2024-12-09 13:58:02 字数 287 浏览 1 评论 0原文

FormCollection fc = Application.OpenForms;

foreach (Form frm in fc)
{
//iterate through
}

或者,

Form fc = Application.OpenForms["FORMNAME"]; if (fc != null) fc.Close(); fm.Show();

但这在紧凑框架 3.5 中不起作用。我如何检查表单是否已在 CF 3.5 中打开?

FormCollection fc = Application.OpenForms;

foreach (Form frm in fc)
{
//iterate through
}

OR

Form fc = Application.OpenForms["FORMNAME"]; if (fc != null) fc.Close(); fm.Show();

but non of this works in compact framework 3.5. How can i check if form is already opened in CF 3.5?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゝ偶尔ゞ 2024-12-16 13:58:02

正如@Barry 所写,你必须自己做。最简单的方法是使用字典。您的密钥可以是表单的类型、名称或您需要的任何内容。

private static readonly Dictionary<string, MyForm> _dict 
    = new Dictionary<string, MyForm>();

public MyForm CreateOrShow(string formName)
{
    Form f = null;
    if (!_dict.TryGetValue(formName, out f))
    {
        f = new MyForm();
        _dict.Add(formName, f);
    } 
    return f;
}

或者,如果您想支持多种表单类型并希望避免强制转换,请使用泛型方法:

private static readonly Dictionary<string, Form> _dict 
    = new Dictionary<string, Form>();

public T CreateOrShow<T>(string formName) where T : Form, new()
{
    Form f = null;
    if (!_dict.TryGetValue(formName, out f))
    {
        f = new T();
        _dict.Add(formName, f);
    }
    return (T)f;
}

public T CreateOrShow<T>(string formName, Func<T> ctor) where T : Form
{
    Form f = null;
    if (!_dict.TryGetValue(formName, out f))
    {
        f = ctor();
        _dict.Add(formName, f);
    }
    return (T)f;
}

有两种泛型重载。其中之一的使用方式如下:

// use this if MyFormType has a parameterless constructor
var form = CreateOrShow<MyFormType>("Form1");

或者,如果您需要在初始化期间将参数传递给表单:

// use this if MyFormType accepts parameters in constructor
var form = CreateOrShow<MyFormType>("Form1", () => new MyFormType(someData));

As @Barry wrote, you will have to do it yourself. Easiest way is to use a dictionary. Your key can be a form's type, its name, or whatever you need.

private static readonly Dictionary<string, MyForm> _dict 
    = new Dictionary<string, MyForm>();

public MyForm CreateOrShow(string formName)
{
    Form f = null;
    if (!_dict.TryGetValue(formName, out f))
    {
        f = new MyForm();
        _dict.Add(formName, f);
    } 
    return f;
}

Or, if you want to support multiple form types and want to avoid casting, use a generic method:

private static readonly Dictionary<string, Form> _dict 
    = new Dictionary<string, Form>();

public T CreateOrShow<T>(string formName) where T : Form, new()
{
    Form f = null;
    if (!_dict.TryGetValue(formName, out f))
    {
        f = new T();
        _dict.Add(formName, f);
    }
    return (T)f;
}

public T CreateOrShow<T>(string formName, Func<T> ctor) where T : Form
{
    Form f = null;
    if (!_dict.TryGetValue(formName, out f))
    {
        f = ctor();
        _dict.Add(formName, f);
    }
    return (T)f;
}

There are two generic overloads. One of them is used like this:

// use this if MyFormType has a parameterless constructor
var form = CreateOrShow<MyFormType>("Form1");

or, if you need to pass parameters to your form during init:

// use this if MyFormType accepts parameters in constructor
var form = CreateOrShow<MyFormType>("Form1", () => new MyFormType(someData));
悲喜皆因你 2024-12-16 13:58:02

Compact Framework 中不存在 Application.OpenForms 集合。

您必须推出自己的收藏并以这种方式跟踪它们。

这里有很好的教程解释了如何实现这一点。

The Application.OpenForms collection doesn't exist in the Compact Framework.

You would have to roll your own collection and keep track of them that way.

There is nice tutorial here that explains how to achieve this.

怪我鬧 2024-12-16 13:58:02

您可以在所有表​​单上都有一个静态布尔字段。然后,您必须创建几个方法来切换它,并将 Opening 和 Closing 事件处理程序绑定到它。诚然,这不是最优雅的解决方案。

partial class Form1 : Form
{
    static bool IsFormOpen;

    public Form1()
    {
        InitializeComponent();
        this.Load += SignalFormOpen;
        this.FormClosing += SignalFormClosed;
    }

    void SignalFormOpen(object sender, EventArgs e)
    {
        IsFormOpen = true;
    }

    void SignalFormClosed(object sender, EventArgs e)
    {
        IsFormOpen = false;
    }
}

或者在某个地方创建一个表单集合,每次加载表单时都会在其中存储对其自身的引用,并对其进行迭代以检查表单是否打开/存在

You could have a static boolean field on all your forms. Then you'd have to create a couple methods to toggle it, and bind the Opening and Closing event handlers to it. Not the most elegant solution admittedly.

partial class Form1 : Form
{
    static bool IsFormOpen;

    public Form1()
    {
        InitializeComponent();
        this.Load += SignalFormOpen;
        this.FormClosing += SignalFormClosed;
    }

    void SignalFormOpen(object sender, EventArgs e)
    {
        IsFormOpen = true;
    }

    void SignalFormClosed(object sender, EventArgs e)
    {
        IsFormOpen = false;
    }
}

Either that or make a collection of forms somewhere, and each time a form loads make it store a reference to itself in it, and iterate against that to check if the form is open/exists

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文