472,107 Members | 1,236 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,107 software developers and data experts.

OnPaint Not Getting Called on Invalidate()

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??

Jun 20 '07 #1
1 3476

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

On Wed, 20 Jun 2007 08:12:03 -0700, "se**@rlc.com" <se**@rlc.com>
wrote:
>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??
Jun 20 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by NewSun | last post: by
11 posts views Thread by Sagaert Johan | last post: by
8 posts views Thread by Mark Johnson | last post: by
5 posts views Thread by Ron Vecchi | last post: by
3 posts views Thread by Colin McGuire | last post: by
14 posts views Thread by raylopez99 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.