Create forms in code but can't 'paint' them

I can create forms in code OK but if I draw graphics on them they don't refresh automatically.  If they are resized or another form is displayed over them then the graphics are wiped out.  If I could put the code in the form's paint method I know it would refresh automatically but I can't access the paint method because the sub form_paint() doesn't exist.  Is there some other way of making a form created in code repaint  

Answer this question

Create forms in code but can't 'paint' them

  • Lalaa

    Okay, you said that form_paint() does not exist. I don't understand this. You can override the OnPaint method, as I have done below. My rectangle is never wiped out.

    protected override void OnPaint(PaintEventArgs e)

    {

    Graphics g = e.Graphics;

    g.DrawRectangle(new Pen(new SolidBrush(Color.Red)), new Rectangle(20, 20, 40, 40));

    }

     



  • Primillo

    Thanks for the suggestion but I would have to call the invalidate method wouldn't I   It won't make the redraw automatic as it is when the drawing is done in the paint method. 
  • beelzeboris

    form_paint does not exist because I have created the form in code. 

    For instance, here I create five forms.  I can then draw on the forms but if I move one form on top of another anything I have drawn gets wiped out. 

    For i = 1To 5

    Dim inst As New graphicsForm

    inst.name = "Window" & Str$(i)

    inst.form = New Form

    graphicsForms.Add(inst, CStr(i))

    Next i

     

     


  • Xanthius

    You can use the Invalidate() method. This will cause a redraw.

    Regards,

    Deepak



  • Create forms in code but can't 'paint' them