【问题标题】:I cannot create a label for my application compiled by CodeDom我无法为 CodeDom 编译的应用程序创建标签
【发布时间】:2018-09-13 19:28:42
【问题描述】:

我正在用 codedom 制作一个简单的表单编译器。一切都很好,表单运行良好,但我可以创建一个“标签”,这是我的应用程序的反编译代码:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Neutron
{
public class NeutronX : Form
{
    private static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new NeutronX());
    }

    public NeutronX()
    {
        this.InitializeComponent();
    }
    private void InitializeComponent()
    {
        this.label1 = new Label();
        base.SuspendLayout();
        base.AutoScaleDimensions = new SizeF(6f, 13f);
        base.AutoScaleMode = AutoScaleMode.Font;
        this.BackColor = Color.FromArgb(255, 255, 255);
        base.ClientSize = new Size(400, 378);
        base.MaximizeBox = false;
        base.Name = "Form";
        base.ShowIcon = false;
        this.Text = "Form";
        this.ForeColor = Color.Black;
        base.ResumeLayout(false);
        base.PerformLayout();
        this.label1.AutoSize = true;
        this.label1.Location = new Point(187, 62);
        this.label1.Name = "label1";
        this.label1.Size = new Size(49, 21);
        this.label1.TabIndex = 0;
        this.label1.Text = "label1";
        this.label1.TextAlign = ContentAlignment.TopLeft;
        this.label1.BackColor = Color.FromArgb(255, 128, 64);
        this.label1.ForeColor = Color.FromArgb(0, 0, 0);
        this.label1.Visible = true;
    }
    public Label label1;
   }
}

我做错了什么?我测试了一切,但一无所获......

【问题讨论】:

  • this.label1不存在,需要先添加一个类成员Label label1。这同样适用于您添加到表单的任何控件。
  • @RonBeyer 我已经添加了它并命名为“public Label label1;”在代码的下方
  • 它会给你什么错误?
  • 我没有看到this.Controls.Add(this.label1)
  • @ZorgoZ 是对的,如果它没有出现在表单上,​​但没有错误,则需要将其添加到控件集合中。

标签: c# winforms codedom


【解决方案1】:

只是把它作为答案。仅创建控件是不够的,您需要将其添加到控件树以由消息循环显示和处理。在您的情况下,表单控制列表。只需添加:

 this.Controls.Add(this.label1);

【讨论】:

  • 谢谢,我真的忘了这个:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-04
  • 2023-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多