【发布时间】:2015-12-30 16:27:10
【问题描述】:
我正在尝试以编程方式创建一个 Windows 窗体控件,从头开始不使用 WinForms 设计器,但由于某些奇怪的原因,似乎没有一个控件被初始化。
当控件被拖入设计器时(显然 InitializeComponent() 方法未注释),它们会出现,但在外部以编程方式完成的任何操作都不会出现。
我已经调试过了,代码运行没有任何错误,但是标签没有出现。
问题:我错过了什么吗?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinForm_Console
{
public partial class Form1 : Form
{
public Form1()
{
// InitializeComponent();
Initialize();
}
Label label1 = new Label();
public void Initialize()
{
this.Text = "WinForm Console Application";
SuspendLayout();
label1.AutoSize = true;
label1.Location = new System.Drawing.Point(129, 112);
label1.Name = "label1";
label1.Size = new System.Drawing.Size(35, 13);
label1.TabIndex = 0;
label1.Text = "label1";
label1.Show();
ResumeLayout(false);
PerformLayout();
}
}
}
请注意,在我最初的尝试失败后,其中一些代码是从设计器那里复制的(这是相同的东西,没有额外的信息;初始标签、大小、逻辑挂起等)
【问题讨论】: