Search code examples
c#onpaint

OnPaint does not update


I have a windows form which contains a user control. This user control has the following code:

protected override void OnPaint(PaintEventArgs pe)
{
  base.OnPaint(pe);
  pe.Graphics.DrawRectangle(
       new Pen(Color.Red, 5 + laenge), 
       new Rectangle(
             new Point(50 + leerzeichen, hoehe), 
             new Size(laenge + 20, 20)));
}

and some more code, which is probably not important now. So when I start the programm it draws the red rectangle. All the variables (laenge, leerzeichen, hoehe) are set to 0 at the beginning of the program. Now, when I press a button the variables are changing, but OnPaint does not draw the new rectangle? What could be the problem? Do I have to call OnPaint in some way?


Solution

  • You need to call Invalidate(), after changing variables (it calls OnPaint internaly)