I have the following code:
int a = 0;
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
this.Rows[1].Cells[1].Value = a += 1;
}
and I can see that the variable increases to infinity. I’m using it to paint a graphic making an instance of it and it works fine.
Is this normal? I am creating infinite graph instance? Or I have a problem and I don’t know
When you change the value, the grid needs to re-
Paintitself., thus firing thePaintevent again and re-executing your code.This behavior is by design.
In general, you should never change external state in a
Painthandler; drawing code should be idempotent (other than the providedGraphics).Paintevents are unpredicable and will fire very often.