在窗口代码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()进行界面初始化之后添加自己的初始化代码。
本文介绍了如何在WindowsForms应用程序的Form1.cs中,使用C#在InitializeComponent()方法之后添加自定义Button控件。着重强调了初始化界面后添加代码的重要性。
1338

被折叠的 条评论
为什么被折叠?



