OnPaint Not Getting Called on Invalidate()

S

sean

I'm trying to create "rubber-band" rectangles by overriding the
OnPaint method to place rectangles on top of all graphic controls, but
when I call Me.Invalidate() (when the user moves the mouse), OnPaint
is not getting called... Here is the relevent code:

I'm trying to create a "rubber band" rectangle effect on my form.

Private Sub HighlightSectionOfTrack(ByVal sender As Object, ByVal e
As
System.Windows.Forms.MouseEventArgs) Handles GraphicsPB.MouseMove

If LeftMouseButtonDown Then
DragEndPoint = New Point(e.X, e.Y)
Me.Invalidate()
End If
End Sub


Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs) MyBase.OnPaint(e)

If Math.Abs(DragStartPoint.X - DragEndPoint.X) > 1 And
Math.Abs(DragStartPoint.Y - DragEndPoint.Y) > 1 And
LeftMouseButtonDown Then
Dim XLoc As Integer = Math.Min(DragStartPoint.X +
GraphicsPB.Location.X, DragEndPoint.X + GraphicsPB.Location.X)
Dim YLoc As Integer = Math.Min(DragStartPoint.Y +
GraphicsPB.Location.Y, DragEndPoint.Y + GraphicsPB.Location.Y)
e.Graphics.DrawRectangle(Pens.Red, XLoc, YLoc,
CInt(Math.Abs(DragStartPoint.X - DragEndPoint.X)),
CInt(Math.Abs(DragStartPoint.Y - DragEndPoint.Y)))
End If
End Sub


The OnPaint method is not getting called when Me.Invalidate is
called!!!! I tried calling Me.Refresh() to no avail also. I know this
because I put an if statement inside the OnPaint method and put a
breakpoint there... What gives??
 
J

Joergen Bech

There should not be any problems.

My guess is that you are handling MouseMove events for a
PictureBox (GraphicsPB) child control but invalidating the form
(Me.Invalidate) instead of the picturebox control
(GraphicsPB.Invalidate)? Invalidating the form does not invalidate
the child controls. Start a new project, add a picturebox to a form,
then run the code below: Only the form receives paint events
when the form is invalidated by the timer. The picturebox is only
invalidated if you move something else over it and exposes it again.

One other thing: Your Abs(startpos-endpos) condition is written
in such a way that a rectangle won't appear until it has a size of
at least 3x3 pixels. Sure this is what you want rather than perhaps
2x2 pixels or even 1x2 or 2x1 pixels?

Regards,

Joergen Bech

---snip---

Public Class Form1
Private WithEvents invalidateTimer As Timer

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
Debug.WriteLine("OnPaint")
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
invalidateTimer = New Timer
With invalidateTimer
.Interval = 500
.Enabled = True
End With
End Sub

Private Sub invalidateTimer_Tick(ByVal sender As Object, ByVal e
As System.EventArgs) Handles invalidateTimer.Tick
Me.Invalidate()
End Sub

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Debug.WriteLine("PictureBox1_Paint")
End Sub

End Class

---snip---
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top