OnPaint override causing graphics to leave artifacts when resized

  • Thread starter Thread starter Brian Henry
  • Start date Start date
B

Brian Henry

I am trying to draw a rectangle that sizes to the size of a user control...
which the user control is docked to the form... when i resize the form it
redraws but the old lines are still left on the screen makeing garbage
images and lines... how do you get the OnPaint event to draw correctly? this
is my code right now for it
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)



mybase.OnPaint(e)

e.Graphics.FillRectangle(New SolidBrush(Color.Red), Me.Top, Me.Left,
Me.Width, Me.Height)

e.Graphics.DrawRectangle(New Pen(Color.Gray), Me.Top, Me.Left, Me.Width -
10, 40)


End Sub



i tried moveing the base class call from the top to the bottom that made no
diffrence.. its a similar effect to drawing a line but not eraseing the
previous line while draging it... leaves the old lines in place.. thanks
 
i added this
Protected Overrides Sub OnResize(ByVal e As System.EventArgs)

Invalidate()



End Sub



and it works ok i think, is this correct? thanks
 
Brian,
That's one way, you can also use the following in the constructor:

' Stop the flicker
Me.SetStyle(ControlStyles.UserPaint, True)
Me.SetStyle(ControlStyles.DoubleBuffer, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.ResizeRedraw, True)
Me.UpdateStyles()

The ControlStyles.ResizeRedraw specifically handles the resizing, while the
other three handle "flickering".

Hope this helps
Jay
 
even better, thanks!


Jay B. Harlow said:
Brian,
That's one way, you can also use the following in the constructor:

' Stop the flicker
Me.SetStyle(ControlStyles.UserPaint, True)
Me.SetStyle(ControlStyles.DoubleBuffer, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.ResizeRedraw, True)
Me.UpdateStyles()

The ControlStyles.ResizeRedraw specifically handles the resizing, while the
other three handle "flickering".

Hope this helps
Jay
 
Back
Top