Form controls not showing

122 Views Asked by At

I'm making a console application that shows a form. I created the form from scratch. When I run the program, the form shows, but the controls I added don't show.

My code:

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);
        }
    }
}
2

There are 2 best solutions below

0
On BEST ANSWER

You're never calling your FrmLogin.Frm method. If you intend this to be a constructor, drop the void and rename it to FrmLogin, like so:

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);
}

If you want instead to call it from the constructor, add a constructor called FrmLogin and have it call Frm, like so:

public FrmLogin()
{
    Frm();
}
0
On

Open a new windows forms application and observe source codes from form.designer.cs and program.cs You will see where you have been mistaken.