My Button is not showing up in C# (Visual Studio Code)

I am trying to add a clickable button on a Form but is not showing up. I am working on Visual Studio Code and I am using the Designers code.

I have tried changing its attributes, size and position but nothing happens, what may be wrong?

    this.components = new System.ComponentModel.Container();
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(400, 620);
    this.StartPosition = FormStartPosition.CenterScreen;
    this.Text = "FORMULARIO";
    this.Controls.Add(label1);
    this.Controls.Add(txtbx1);
    this.Controls.Add(label2);
    this.Controls.Add(txtbx2);
    this.Controls.Add(label3);
    this.Controls.Add(txtbx3);
    this.Controls.Add(label4);
    this.Controls.Add(txtbx4);
    this.Controls.Add(btn);
    
    
    btn = new Button();
    btn.Text = "Submit";
    btn.Location = new Point(400, 100);
    btn.Size = new Size(100, 100);
    btn.BackColor = Color.Gray;
    btn.ForeColor = Color.Yellow;
    btn.Font = new Font("Verdana", 16);
    btn.Click += new EventHandler(Button_Click);

}
private void Button_Click(object sender, EventArgs e)
{
    MessageBox.Show("Thank You! ");
    this.Close();
}
private System.Windows.Forms.Button add;

#endregion

}

>Solution :

this.Controls.Add(btn);


btn = new Button();

You are adding btn to Controls collection first. (Whereever you are initializing your btn variable). And then you create new Button(). Try to set up your button before adding to collection.

Leave a Reply