首页 > 解决方案 > C# WinForms: Constructing an object with label/textbox properties in a form does not result in those properties appearing in the form. Why?

问题描述

What I'm doing is: I have an object which initializes its textbox/label contents in the constructor, however when the object is constructed in its intended form I can't make the contents appear on the form. An example:

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

I tried adding visibility tags but that wasn't it. What am I doing wrong?

标签: c#winformsvisibility

解决方案


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

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


推荐阅读