表单控件不显示

Form controls not showing

我正在制作一个显示表单的控制台应用程序。我从头开始创建表格。当我运行程序时,窗体显示,但我添加的控件不显示。

我的代码:

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

namespace form
{
    public class main
    {
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new FrmLogin());
        }
    }

    public class FrmLogin : Form
    {
        public void Frm()
        {
            this.Size = new Size(400, 600);
            Button btn = new Button();
            btn.Text = "Something";
            btn.Size = new Size(10, 10);
            btn.Location = new Point(10, 10);
            btn.UseVisualStyleBackColor = true;
            this.Controls.Add(btn);
        }
    }
}

您永远不会调用 FrmLogin.Frm 方法。如果您打算将其作为构造函数,请删除 void 并将其重命名为 FrmLogin,如下所示:

public FrmLogin()
{
    this.Size = new Size(400, 600);
    Button btn = new Button();
    btn.Text = "Something";
    btn.Size = new Size(10, 10);
    btn.Location = new Point(10, 10);
    btn.UseVisualStyleBackColor = true;
    this.Controls.Add(btn);
}

如果您想从构造函数中调用它,请添加一个名为 FrmLogin 的构造函数并让它调用 Frm,如下所示:

public FrmLogin()
{
    Frm();
}

打开一个新的 windows 表单应用程序并观察来自 form.designer.cs 和 program.cs 的源代码,您会发现哪里出错了。