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

  • Thursday, December 15, 2005 9:20 PM
     
     
    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? 

All Replies

  • Thursday, December 15, 2005 10:24 PM
     
     

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

    Regards,

    Deepak

  • Thursday, December 15, 2005 10:49 PM
     
     
    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. 
  • Thursday, December 15, 2005 11:26 PM
     
     

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

    }

     

  • Friday, December 16, 2005 1:28 PM
     
     

    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