【发布时间】:2015-06-13 19:42:01
【问题描述】:
我想当我点击按钮时,在表单中添加一个矩形
我可以在表单中添加我想要多少,但我无法通过单击按钮添加矩形等形状,我搜索了它,但我没有找到解决方案
有人知道怎么做吗?
这是我的表单绘制代码
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));
}
这是我的按钮代码
private void button1_Click(object sender, EventArgs e)
{
this.Paint += Form1_Paint;
}
但是当我单击按钮时它不起作用。为什么它不工作?
【问题讨论】:
-
您的按钮单击只是添加了一个事件处理程序,它不会导致该事件被触发。
-
将
this.Paint += Form1_Paint;替换为Invalidate();确保已连接Paint 事件。 (一次)。要添加越来越多的 Rectangles 需要将它们的坐标添加到 List!
标签: c# winforms events drawing