Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/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
Templates 在windows窗体控件上使用模板 public static T newitem(表单控件,int Left,int Top,string Text=”“,bool Visible=true),其中T:new() { T a=新的T(); a、 左=左; a、 顶部=顶部; a、 文本=文本; a、 可见=可见; 控制。控制。添加(a); 返回a; }_Templates - Fatal编程技术网

Templates 在windows窗体控件上使用模板 public static T newitem(表单控件,int Left,int Top,string Text=”“,bool Visible=true),其中T:new() { T a=新的T(); a、 左=左; a、 顶部=顶部; a、 文本=文本; a、 可见=可见; 控制。控制。添加(a); 返回a; }

Templates 在windows窗体控件上使用模板 public static T newitem(表单控件,int Left,int Top,string Text=”“,bool Visible=true),其中T:new() { T a=新的T(); a、 左=左; a、 顶部=顶部; a、 文本=文本; a、 可见=可见; 控制。控制。添加(a); 返回a; },templates,Templates,我想编写一个函数,将任何windows窗体控件添加到我的窗体中。当我试图到达a.xxx时,我得到一个错误。 错误:“T”不包含“Left”的定义,并且找不到接受类型为“T”的第一个参数的扩展方法“Left”。 如何解决此问题?添加一个附加约束,其中T:Control,因为您只能向表单添加控件。这将T限制为Control类型,从而确保Left属性可用(处于控制状态) public static T newitem<T>(Form Control, int Left, int T

我想编写一个函数,将任何windows窗体控件添加到我的窗体中。当我试图到达a.xxx时,我得到一个错误。 错误:“T”不包含“Left”的定义,并且找不到接受类型为“T”的第一个参数的扩展方法“Left”。
如何解决此问题?

添加一个附加约束,其中
T:Control
,因为您只能向表单添加控件。这将
T
限制为
Control
类型,从而确保
Left
属性可用(处于控制状态)

    public static T newitem<T>(Form Control, int Left, int Top, string Text = "", bool Visible = true) where T : new()
    {
        T a = new T();
        a.Left = Left;
        a.Top = Top;
        a.Text = Text;
        a.Visible = Visible;
        Control.Controls.Add(a);
        return a;
    }