gpt4 book ai didi

c# - 动态添加的标签不出现

转载 作者:行者123 更新时间:2023-12-03 02:16:58 25 4
gpt4 key购买 nike

我有一些标签和一堆文本框,我想将它们动态添加到面板中。文本框已添加正常并且完全可见,但标签却不然。这是我用来添加标签的代码。

该语言是为 .NET 3.5 WinForms 应用程序编写的 C#。

Label lblMotive = new Label();
lblMotive.Text = language.motive;
lblMotive.Location = new Point(0, 0);

Label lblDiagnosis = new Label();
lblDiagnosis.Text = language.diagnosis;
lblDiagnosis.Location = new Point(20, 0);

panelServiceMotive.Controls.Add(lblMotive);
panelServiceMotive.Controls.Add(lblDiagnosis);

panelServiceMotive 是应该显示标签以及前面提到的文本框的面板控件。 language 是一个自己编写的 Language 类的对象,它工作正常,与这里无关。

我希望这些信息足以获得帮助。

最佳答案

看起来主要问题是添加到面板的控件的位置。 Location 属性保存控件左上角相对于父控件(在其中添加子控件的控件)左上角的坐标。查看您的代码,您似乎将一个控件添加到另一个控件之上。请注意,您始终设置lblDiagnosis.Location = new Point(0, 0);。如果您从代码添加控件,您添加的第一个控件将覆盖您在同一位置添加的所有其他控件(与使用设计器时不同)。

您可以尝试这样的方法来检查标签是否正常:

Label lblMotive = new Label();
lblMotive.Text = language.motive;
lblMotive.Location = new Point(0, 40);

Label lblDiagnosis = new Label();
lblDiagnosis.Text = language.diagnosis;
lblDiagnosis.Location = new Point(0, lblMotive.Location.Y + lblMotive.Size.Height + 10);

panelServiceMotive.Controls.Add(lblMotive);
panelServiceMotive.Controls.Add(lblDiagnosis);

关于c# - 动态添加的标签不出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13699821/

25 4 0