我想当我点击按钮时,向表单添加一个矩形
我可以在表单中添加我想要多少但我无法通过点击按钮添加像矩形这样的形状,我搜索了它,但我没有找到解决方案
这里有人知道怎么做吗?
这是我在表单绘制中的代码
private void Form1_Paint(object sender, PaintEventArgs e)
{
locationX = locationX + 20;
locationY = locationY + 20;
e.Graphics.DrawRectangle(Pens.Black,
new Rectangle(10 + locationX, 10 + locationY, 50, 30));
}
Run Code Online (Sandbox Code Playgroud)
这是我的按钮代码
private void button1_Click(object sender, EventArgs e)
{
this.Paint += Form1_Paint;
}
Run Code Online (Sandbox Code Playgroud)
但是当我单击按钮时它不起作用。为什么它不工作?