Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在单击按钮时绘制矩形?_C#_Winforms_Events_Drawing - Fatal编程技术网

C# 如何在单击按钮时绘制矩形?

C# 如何在单击按钮时绘制矩形?,c#,winforms,events,drawing,C#,Winforms,Events,Drawing,我想在单击按钮时,在表单中添加一个矩形 我可以在表单中添加我想要多少,但我不能通过单击按钮添加矩形形状,我搜索了它,但没有找到解决方案 有人知道怎么做吗 这是我在form paint中的代码 private void Form1_Paint(object sender, PaintEventArgs e) { locationX = locationX + 20; locationY = locationY + 20;

我想在单击按钮时,在表单中添加一个矩形
我可以在表单中添加我想要多少,但我不能通过单击按钮添加矩形形状,我搜索了它,但没有找到解决方案
有人知道怎么做吗

这是我在form paint中的代码

    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;
    }

但当我点击按钮时,它不工作。为什么它不起作用

要调用方法,需要括号

private void button1_Click(object sender, EventArgs e)
{
    Form1_Paint(sender, e);
}
线路

 this.Paint += Form1_Paint;
将表单的事件
Paint
与函数Form1\u Paint关联它不会触发它。这是你只想做一次的事情,而不是每次按下按钮

要触发
Paint
事件,通常的方法是调用
Form
类的
Invalidate()
方法。实际上,Invalidate是一种方法。但是
Form
派生自
Control
,因此我们也可以访问
Form
中的方法

因此,在Windows窗体中触发重新绘制的正确方法是将subscribe放在Load方法中:

private void Form1_Load(object sender, EventArgs e)
{
    this.Paint += Form1_Paint;
}
它应该已经隐藏在自动生成的代码中。 您的方法
Form1\u Paint
正常

最后,按钮点击方法应为:

private void button1_Click(object sender, EventArgs e)
{
    this.Invalidate(); // force Redraw the form
}
:

Invalidate():使控件的整个表面无效,并使控件重新绘制

编辑:

使用此方法,一次只能绘制一个矩形,因为整个曲面都已重绘,因此曲面将完全擦除,然后它只绘制Form1_Paint方法中要求的内容

对于如何绘制多个矩形的答案,您应该创建一个矩形列表。在每次单击按钮时,您将向列表中添加一个矩形,然后重新绘制所有矩形

List<Rectangle> _rectangles = new List<Rectangle>();
private void button1_Click(object sender, EventArgs e)
{
    locationX = locationX + 20;
    locationY = locationY + 20;
    var rectangle = new Rectangle(locationX, locationY, 50, 30));
    this._rectangles.Add(rectangle);
    this.Invalidate(); // force Redraw the form
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
    foreach(var rectangle in this._rectangles)
    {
        e.Graphics.DrawRectangle(Pens.Black, rectangle);
    }
}
List_矩形=新列表();
私有无效按钮1\u单击(对象发送者,事件参数e)
{
位置X=位置X+20;
位置Y=位置Y+20;
var矩形=新矩形(位置X,位置Y,50,30));
这个._矩形。添加(矩形);
this.Invalidate();//强制重新绘制表单
}
私有void Form1_Paint(对象发送器、PaintEventArgs e)
{
foreach(此._矩形中的变量矩形)
{
e、 图形.绘图矩形(钢笔.黑色,矩形);
}
}

您的按钮单击只是添加了一个事件处理程序,它不会触发该事件。替换
this.Paint+=Form1\u Paint通过
无效()确保已连接绘制事件。(一次)。要添加越来越多的矩形,需要将其坐标添加到列表中!这将不起作用(也无法编译),因为
Paint
需要一个非常特殊的参数<代码>发件人
显然不应该是按钮,而应该是表单
。但是,
PaintEventArgs
不是你应该尝试“创建”的东西…谢谢,但是还有一个问题,现在当我单击按钮时,它工作了,但它删除了它制作的最后一个形状,我希望在单击“不删除”之前制作矩形,每次单击它工作时只对它们进行一次,非常感谢,我想这是“从按钮开始制作”的第一个答案;)请把我的问题投赞成票