【问题标题】:Open all closed forms in my project one at a time一次打开我项目中所有已关闭的表单
【发布时间】:2013-03-25 19:10:48
【问题描述】:

我发现下面的代码将遍历我项目中所有关闭的表单并打开一个 MessageBox 并显示表单名称。

但是,我将如何修改它而不是显示 MessageBox;它实际上会一个一个地打开每个封闭的表格吗?我更喜欢使用 ShowDialog 或类似的东西,所以每个表单一次只能打开 1 个,而不是一次打开。只要我关闭一个表单,下一个表单就会打开,以此类推。

//http://kellyschronicles.wordpress.com/2011/08/06/show-all-forms-in-a-project-with-c/
System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetEntryAssembly();
Type[] Types = myAssembly.GetTypes();
foreach (Type myType in Types)
{
   if (myType.BaseType == null) continue;

       if (myType.BaseType.FullName == "System.Windows.Forms.Form")
       {
           //Application.Run(myType.Name());  //This does not work
           MessageBox.Show(myType.Name);
       }   

}  

【问题讨论】:

    标签: c# winforms forms


    【解决方案1】:

    试试这个:

    var form = (Form)Activator.CreateInstance(myType);
    form.ShowDialog();
    

    您可以将它与这样的默认构造函数或带参数的构造函数一起使用,但这有点棘手。
    更多内容见:Activator.CreateInstance Method

    【讨论】:

    • 错误:错误 5“System.Activator”不包含“创建”的定义
    • @fraXis,我知道我刚刚解决了这个问题,抱歉。
    • 我将其更改为 Activator.CreateInstance 并且除了 form.ShowDialog() 现在没有出错:错误 5 'object' does not contain a definition for 'ShowDialog' and no extension可以找到接受“object”类型的第一个参数的方法“ShowDialog”(您是否缺少 using 指令或程序集引用?)
    • @fraXis,你必须像我在更新版本中那样将 i 转换为 Form
    【解决方案2】:
    System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetEntryAssembly();
    Type[] Types = myAssembly.GetTypes();
    foreach (Type myType in Types)
    {
       if (myType.BaseType == null) continue;
    
           if (myType.BaseType.FullName == "System.Windows.Forms.Form")
           {
               //Application.Run(myType.Name());  //This does not work
               //MessageBox.Show(myType.Name);
               var myForm = (System.Windows.Forms.Form)
                      Activator.CreateInstance(myAssembly.Name, myType.Name);
               myForm.Show();
           }   
    
    }  
    

    【讨论】:

      【解决方案3】:

      试试这个:

        if (myType.BaseType.FullName == "System.Windows.Forms.Form")
         {
             //Application.Run((Form)myType);
             Application.Run((Form)Activator.CreateInstance(myType));
         }  
      

      【讨论】:

      • 不起作用:错误 5 无法将类型“System.Type”转换为“System.Windows.Forms.Form”
      【解决方案4】:

      您需要向 Application.Run 方法提供表单的新实例。 尝试将其转换为形式并创建新实例。 像这样的:

      public Form TryGetFormByName(string frmname)
      {
         var formType = Assembly.GetExecutingAssembly().GetTypes().Where(a => a.BaseType == typeof(Form) && a.Name == frmname).FirstOrDefault();
         if (formType == null) // If there is no form with the given frmname
             return null;
         return (Form)Activator.CreateInstance(formType);
      }
      

      来自这个帖子Winforms, get form instance by form name

      【讨论】:

      • 我将如何使用 'Type myType' 来做到这一点?如何将其转换为表单实例? Application.Run((Form)myType.Name());对我不起作用。
      • 已更新以使其清晰。请在墨水中查看更多详细信息
      猜你喜欢
      • 1970-01-01
      • 2020-03-01
      • 2010-10-05
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多