WinForm表单启动器(Application.OpenForms)

本文关键字:Application OpenForms 表单 启动 WinForm | 更新日期: 2023-09-27 18:09:02

我用以下代码调用我的winforms:

try
        {
            if (Application.OpenForms.OfType<frmBackupManager>().Count() > 0)
            {
                if (Application.OpenForms["frmBackupManager"].WindowState == FormWindowState.Minimized)
                {
                    Application.OpenForms["frmBackupManager"].WindowState = FormWindowState.Normal;
                    Application.OpenForms["frmBackupManager"].BringToFront();
                }
                else
                {
                    Application.OpenForms["frmBackupManager"].BringToFront();
                }
            }
            else
            {
                // Show Backup Manager                    
                frmBackupManager myBackupManager;
                myBackupManager = new frmBackupManager();
                myBackupManager.StartPosition = FormStartPosition.Manual;
                myBackupManager.Location = new Point(this.Location.X + this.Width / 2 - myBackupManager.Width / 2, this.Location.Y + this.Height / 2 - myBackupManager.Height / 2);
                myBackupManager.Show();
            }
        }
        catch (Exception ex)
        {
            MessageBoxEx.Show("Unable to perform requested action: " + ex.Message.ToString(), "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

我想从一个方法或类调用这个,但我一直得到一个错误的类型或命名空间名称'FormName'找不到(你缺少使用指令或程序集引用吗?)在每个使用FormName的地方

private void btnBackupManager_Click(object sender, EventArgs e)
    {
        // Launch Backup Manager Form
        LaunchForm("frmBackupManager", "MyBackupManager");
    }
    private void LaunchForm(string FormName, string MyForm)
    {
        try
        {
            if (Application.OpenForms.OfType<FormName>().Count() > 0)
            {
                if (Application.OpenForms[FormName].WindowState == FormWindowState.Minimized)
                {
                    Application.OpenForms[FormName].WindowState = FormWindowState.Normal;
                    Application.OpenForms[FormName].BringToFront();
                }
                else
                {
                    Application.OpenForms[FormName].BringToFront();
                }
            }
            else
            {
                // Show Backup Manager                    
                FormName myFormName;
                myFormName = new FormName();
                myFormName.StartPosition = FormStartPosition.Manual;
                myFormName.Location = new Point(this.Location.X + this.Width / 2 - myFormName.Width / 2, this.Location.Y + this.Height / 2 - myFormName.Height / 2);
                myFormName.Show();
            }
        }
        catch (Exception ex)
        {
            MessageBoxEx.Show("Unable to perform requested action: " + ex.Message.ToString(), "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

WinForm表单启动器(Application.OpenForms)

就像其他海报提到的,你试图使用FormName变量作为类型。

试试这样:

private static void LaunchForm<T>() where T : Form, new()
{
  var existing = Application.OpenForms.OfType<T>().FirstOrDefault();
  if(existing != null) {
    // Make sure existing is visible.
  } else {
    var newForm = new T();
    // Initialize it here.
    newForm.Show();
  }
}

您的问题是使用变量作为类型。例如,这一行:

if (Application.OpenForms.OfType<FormName>().Count() > 0)

FormName可能确实包含类型的名称,但它本身不是类型。

有此问题的其他行包括:

FormName myFormName;
myFormName = new FormName();

你可以使用反射来解决这个问题:

if(Application.OpenForms.Where(form => form.GetType().Name == FormName).Count() > 0)
Form myFormName;
myFormName = (Form)Assembly.GetCallingAssembly().
    GetType("YourNamespace." + FormName).
    GetConstructor(new Type[] {}).
    Invoke(new object[] {});
  • 你的泛型函数可以有一个Form类型的输入参数(所有Win-Forms都是从这个类派生出来的)
  • 例如:-

  • 这样你就可以在任何需要的地方使用对象引用

  • 并且只访问name (Form类的name属性;例如:- formToOpen.Name),如适用和相关
  • 保持实现,强类型
  • 所以所需的函数的实现可以如下:

    private void LaunchForm(Form formToOpen)
    {
        try
        {
            if (formToOpen != null)
            {
                if (Application.OpenForms.OfType<Form>().Count() > 0)
                {
                    if (Application.OpenForms[formToOpen.Name].WindowState == FormWindowState.Minimized)
                    {
                        Application.OpenForms[formToOpen.Name].WindowState = FormWindowState.Normal;
                        Application.OpenForms[formToOpen.Name].BringToFront();
                    }
                    else
                    {
                        Application.OpenForms[formToOpen.Name].BringToFront();
                    }
                }
                else
                {
                    Form formToLaunch;
                    formToLaunch = formToOpen;
                    formToLaunch.StartPosition = FormStartPosition.Manual;
                    formToLaunch.Location = new Point(this.Location.X + this.Width / 2 - formToLaunch.Width / 2, this.Location.Y + this.Height / 2 - formToLaunch.Height / 2);
                    formToLaunch.Show();
                }
            }
        }
        catch (Exception ex)
        {
            MessageBoxEx.Show("Unable to perform requested action: " + ex.Message.ToString(), "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    

    }

您的问题是您使用类型(类名)作为变量。

错误是局部变量名和类名相同。解决这个问题最简单的方法就是将本地变量名重命名为other。

这里,FormType是一个类名,用作类型,但这是错误的类名用法:

if (Application.OpenForms.OfType<FormName>().Count() > 0) 
{
    if (Application.OpenForms[FormName].WindowState == FormWindowState.Minimized) 
    { 
        Application.OpenForms[FormName].WindowState = FormWindowState.Normal; 
        Application.OpenForms[FormName].BringToFront(); 
    } 
    else 
    { 
        Application.OpenForms[FormName].BringToFront(); 
    } 
}

开关:

if (Application.OpenForms.OfType<FormName>().Count() > 0) 
{
    if (Application.OpenForms[childForm].WindowState == FormWindowState.Minimized) 
    { 
        Application.OpenForms[childForm].WindowState = FormWindowState.Normal; 
        Application.OpenForms[childForm].BringToFront(); 
    } 
    else 
    { 
        Application.OpenForms[childForm].BringToFront(); 
    } 
}

其中childForm是方法的参数:

private void LaunchForm(string childForm, string MyForm)    
{
    ...
}