C# WinForms:在表单中构造具有 label/textbox 属性的对象不会导致这些属性出现在表单中。为什么?

C# WinForms: Constructing an object with label/textbox properties in a form does not result in those properties appearing in the form. Why?

我正在做的是:我有一个对象,它在构造函数中初始化它的 textbox/label 内容,但是当对象以其预期形式构造时,我无法使内容出现在表单上.一个例子:

public ProductEntry(int value){
       this.productdesc = new TextBox
       {
        Location = new Point(x,y), 
        Width = 30,
        Height = 30,
       };
       }

我尝试添加可见性标签,但事实并非如此。我做错了什么?

您应该将此控件添加到表单中 - 如下所示:

YourForm.Controls.Add(productdesc);

这里有更详细的描述:https://support.microsoft.com/pl-pl/help/319266/how-to-programmatically-add-controls-to-windows-forms-at-run-time-by-u