Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在类文件中创建控件_C#_Winforms_Class - Fatal编程技术网

C# 在类文件中创建控件

C# 在类文件中创建控件,c#,winforms,class,C#,Winforms,Class,我在C Vis Studio Express中选择了类库选项,以创建一个DLL,其中包含大量常用方法等 我正在尝试在类文件中创建一个文本框,这样当我将dll添加到另一个项目时,我只需键入以下内容: MyControls.CreateTextBox …它将创建一个文本框并返回给我,然后我可以将其添加到表单中 我知道如何创建类等,所以,我的问题是。。。为什么我不能使用System.Windows.Forms是类文件?我的Class1.cs文件中包含以下内容: using System; using

我在C Vis Studio Express中选择了类库选项,以创建一个DLL,其中包含大量常用方法等

我正在尝试在类文件中创建一个文本框,这样当我将dll添加到另一个项目时,我只需键入以下内容:

MyControls.CreateTextBox

…它将创建一个文本框并返回给我,然后我可以将其添加到表单中

我知道如何创建类等,所以,我的问题是。。。为什么我不能使用System.Windows.Forms是类文件?我的Class1.cs文件中包含以下内容:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyControls
{
    public class class1
    {
        public object Create(object control)
        {
            System.Windows.Forms.TextBox t = new System.Windows.Forms.TextBox();
            // textbox properties go here etc...

            return control;
        }
    }
}
但是红色的斜线一直在告诉我名称空间“系统”中不存在名称空间“Windows”的类型。是否缺少程序集引用

我忘了在这里加些什么了吗


谢谢:

听起来好像您缺少对System.Windows.Forms的引用;添加该引用,您的代码应该可以很好地编译

旁注 我确实对你的方法有点好奇:

public object Create(object control)
{
    System.Windows.Forms.TextBox t = new System.Windows.Forms.TextBox();
    // textbox properties go here etc...

    return control;
}
输入参数用于什么?如果你不使用它,就没有必要通过它。此外,由于该方法应该为您创建控件,因此可以将返回类型更改为Control。这将消除为了将其添加到表单而强制转换它的需要。我建议这样设计方法,而不是使用泛型:

public T Create<T>() where T : Control
{
    T control  = Activator.CreateInstance<T>();
    // textbox properties go here etc...

    return control;
}

是的,您必须在项目的引用中添加对winforms dll System.Windows.Forms的引用。当您创建win forms应用程序时,这种情况会自动发生,但因为您只创建了一个dll,所以它不在那里。

添加引用。因为您创建了类库而不是表单项目,所以没有必要的库引用。请转到“项目”菜单>“添加引用…”。。。然后从.NET选项卡中选择System.Windows.Forms。

如上所述,添加对System.Windows.Forms的引用就足够了。
虽然我个人不喜欢在同一个程序集中混合使用公共类和控件,而是让一个或多个项目用于非GUI代码,再加上另一个项目用于纯GUI代码。

@baeltazor:欢迎您:注意更新的代码示例,其中添加了类型约束,我首先忘记了将要使用的输入参数,因此我可以在创建控件之前传递一些属性,以防我只是想创建一个与以前略有不同的控件。。。但是我更喜欢你的方式:预先修改的Create:where约束允许你设置属性和事件控件的所有实例,比如Name、Text、MouseDown:当然,你不能设置特定类型控件的唯一属性:比如:for TextBox:the“MultiLine property”。但是,通过将调用Create的结果分配给强类型变量,您可以自动向上转换.NET 4.0:在早期版本中未测试的结果:例如:TextBox myTextBox=Create;:当然,在这一点上,您可以访问文本框特定的属性、事件等!谢谢BillW::我不太明白为什么这比只使用Textbox或其他控件构造函数更好。。。