0

I need call PaintEvent in while(canDrawing) loop. Where I need use loop? In Event it can't stop drawing, Esc doesn't call.

public Form1()
{
    InitializeComponent();
    penCircle = new Pen(BackColor, radius);
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.FillRectangle(brushPoint, RandomXPosition(), RandomYPosition(), 1, 1);
            e.Graphics.DrawEllipse(penCircle, xPositionCircle, yPositionCircle, radius, radius);
}

private void Esc(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Escape)
    {
        canDrawing = false;
    }
}
Nik P
  • 2,693
  • 9
  • 21
  • You could of course fix it by calling [DoEvents()](https://stackoverflow.com/q/5181777/1997232) in the loop, but... Rather than using tight loop inside paint event, consider to chain `Invalidate()` calls. Event handlers are not supposed to do constant job. Use task, thread, timer for this. – Sinatr Jun 02 '20 at 13:01
  • 1
    `Form1 form = new Form1();` inside paint event is really awkward. – Sinatr Jun 02 '20 at 13:04
  • 1
    heightOfForm = ClientSize.Height; widthOfForm = ClientSize.Width; colorOfCircle = BackColor; – Arsen Dmitriiev Jun 02 '20 at 13:17
  • tnx it`s similar – Arsen Dmitriiev Jun 02 '20 at 13:17
  • 1
    _graphics.Dispose();_ Do not dispose of things you didn't create! - _Form1 form = new Form1();_ This is a new form, not the one your are trying to paint on. - There is no escape from the loop; instead call Invalidate() to trigger the Paint event from an outside loop that also tests the keyboard state. – TaW Jun 02 '20 at 13:31
  • where need use loop? – Arsen Dmitriiev Jun 02 '20 at 13:42
  • 1
    The loop can be anywhere, e.g. in a void PaintLoop method. Just make sure it contains code to test the keyboard. As it is the loop is tight and will not allow anything else.. – TaW Jun 02 '20 at 13:51
  • man TNX!, I wasted 3 hours. – Arsen Dmitriiev Jun 02 '20 at 14:07

0 Answers0