简体   繁体   English

如何检查动态创建的winform实例是否已经在c#中打开

[英]How to check if the dynamically created instance of a winform is already open in c#

I know how to check for the form if it is already open.我知道如何检查表格是否已经打开。 But, I am having a hard time finding the same if the form was created dynamically.但是,如果表单是动态创建的,我很难找到相同的。 How can I find the type of dynamic object?如何找到动态 object 的类型? In the code below, I am trying to find what goes in Application.OpenForms.OfType< ???在下面的代码中,我试图找到 Application.OpenForms.OfType< ??? >().Any(). >()。任何()。 I am sure it is something silly but I can't crack it.我确信这很愚蠢,但我无法破解它。

public static bool OpenForm(string formName)
{
    try
    {
        string namespace = MethodInfo.GetCurrentMethod().ReflectedType.Namespace;
        Assembly assembly = Assembly.GetExecutingAssembly();
        var _form = assembly.CreateInstance($"{namespace}.{formName}") as Form;

        if (Application.OpenForms.OfType<???>().Any())
        {
            //Form is already open, do nothing
            return true;
        }            

        if (_form != null)
            _form.Show();

        return true;
    }
    catch (Exception ex)
    {
        return false;
    }            
}

If you mean to use a generic method to verify whether an instance of a Form type exists and, if it's not, then run it, you can use a generic method and to perform both the check and create the instance:如果您打算使用泛型方法来验证 Form 类型的实例是否存在,如果不存在,则运行它,您可以使用泛型方法并执行检查和创建实例:

Check whether an instance of the type exists:检查该类型的实例是否存在:

Application.OpenForms.OfType<T>() 

Create the instance and show the Form if none that type is already created:如果尚未创建该类型,则创建实例并显示表单:

public static bool OpenForm<T>() where T : Form, new()
{
    if (Application.OpenForms.OfType<T>().Any()) return true;

    try {
        var f = new T();
        f.Show();
        return true;
    }
    catch (Exception) {
        return false;
    }
}

Since you're passing the name of a Form in your code snippet, you might instead check whether an instance of a Form with that name already exists:由于您在代码片段中传递了 Form 的名称,因此您可以检查是否已经存在具有该名称的 Form 实例:

  Application.OpenForms.OfType<Form>().Any(f => f.Name.Equals(formName))
  • If it doesn't, first check whether the assembly actually contains a type with that name (passing true , causes GetType() to throw if it fails to find a match, so it goes to the same exception handler):如果没有,首先检查程序集是否实际上包含具有该名称的类型(传递true ,如果找不到匹配项,则导致GetType()抛出,因此它转到相同的异常处理程序):

     var formType = assembly.GetType(formAsmName, true);

Otherwise, it goes on and creates the instance.否则,它将继续并创建实例。
Next time you try to create an instance of a Form with that name, the first check will return true and won't create a new instance.下次您尝试使用该名称创建 Form 的实例时,第一次检查将返回true并且不会创建新实例。


public static bool OpenForm(string formName)
{
    if (Application.OpenForms.OfType<Form>().Any(f => f.Name.Equals(formName))) return true;

    try {
        var assembly = Assembly.GetExecutingAssembly();
        var formAsmName = $"{assembly.GetName().Name}.{formName}";
        var formType = assembly.GetType(formAsmName, true); // <- Throws

        var form = assembly.CreateInstance(formType.FullName) as Form;
        form.Show();
        return true;
    }
    catch (Exception) {
        // Throw a specific exception instead?
        return false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM