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

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

  • 15 Desember 2005 21:20
     
     
    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? 

Semua Balasan

  • 15 Desember 2005 22:24
     
     

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

    Regards,

    Deepak

  • 15 Desember 2005 22:49
     
     
    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. 
  • 15 Desember 2005 23:26
     
     

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

    }

     

  • 16 Desember 2005 13:28
     
     

    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