2015-09-08 39 views
-5

我有这样的代码:如何将表单转换为类?

主营:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Threading; 

namespace TestApp 
{ 
    class Program 
    { 
     static void Main() 
     { 
      WC W1 = WC.CreateWindow("", ""); 
     } 
    } 
} 

WindowControl:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

public class WC 
{ 
    public static Form CreateWindow(string name, string title) 
    { 
     Form nform = new Form(); 
     nform.Name = name; 
     nform.Text = title; 
     Application.EnableVisualStyles(); 
     Application.Run(nform); 
     return nform; 
    } 
    public static void DeleteWindow(Form name) 
    { 
     name.Close(); 
    } 

}

但我得到这个错误:

Cannot implicitly convert type 'System.Windows.Forms.Form' to 'WC'.

我在做什么错了?

+1

'形式W1 = WC.CreateWindow( “”, “”);'或'VAR W1 = WC.CreateWindow( “” ,“”);'或者只是'WC.CreateWindow(“”,“”);'因为你还没有使用变量。 –

+0

'WC.CreateWindow'返回* Form * not * WC *。所以更改为'表格W1 = WC.CreateWindow(“”,“”);' – Eser

回答

0

你正在尝试做一些奇怪的事情。如果你想创建类似的东西,形成厂,你可以做这样的:

namespace TestApp 
{ 
    class Program 
    { 
     static void Main() 
     { 
      Form W1 = WC.CreateWindow("test1", "...."); 
      var W2 = WC.CreateWindow("test2", "...."); 
     } 
    } 
} 
+1

感谢它现在的作品,再次感谢 – Programmer2120