在窗口代码Form1.cs编辑器中进行代码添加,代码如下(红框和蓝框中的代码):
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 WindowsFormsApp1
{
public partial class Form1 : Form
{
private Button myButton = new Button();
public Form1()
{
InitializeComponent();
//
myButton.Text = "手动添加按钮";
myButton.Location = new Point(150, 150);
myButton.Size = new Size(100, 50);
this.Controls.Add(this.myButton);
}
}
}
运行结果:
强调:
要在InitializeComponent()进行界面初始化之后添加自己的初始化代码。