473,320 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 3576

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: NewSun | last post by:
I draw a grid on a panel.And panel1' property AutoScroll is set true.When HScroollBar is Scroolling,the grid is error. I have rewritten the mathod of WndProc.But the effect is unexpected. How can...
11
by: Sagaert Johan | last post by:
I have made a custom control that draws a rectangle when the mouse is down, and does nothing when the mouse is up. I set/reset a flag in MouseDown/Mouse up and use this to do the drawing in the...
8
by: Mark Johnson | last post by:
Using: VS 2003 NET C# for Framework and Framework Compact Trying : Moving a Card (Bitmap) as in Solitare (PC + WinCe) Version on OnMouseMove Problem : The affected drawing Area by Invalidate (or...
5
by: Ron Vecchi | last post by:
I have a custyom control that has a property which wraps a collection. When I add controls to the collection the OnPaint method of these newly added controls never get fired. But if I take the...
0
by: vooose | last post by:
Consider a UserControl to which you do userControl.Paint += new PaintEventHandler(paint_method) If you don't like that way, and prefer to override onPaint( ) then the problem stated below...
3
by: Colin McGuire | last post by:
Hi, I am just learning "how-to" by reading some of the existing article solutions in this newsgroup. I happened to come across this one. Tom Spink knows what he is talking about and I am wondering...
20
by: BB | last post by:
Hello all, I am trying to override OnPaint in a custom textbox control (so I can drawstring a caption, etc.). In the code below, I get the "painting the form" message as expected, but not the...
4
by: grayaii | last post by:
Hi, I have a simple form that handles all its paint functionality like so: this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true); And the entry point to this...
14
by: raylopez99 | last post by:
KeyDown won't work KeyPress fails KeyDown not seen inspired by a poster here:http://tinyurl.com/62d97l I found some interesting stuff, which I reproduce below for newbies like me. The main...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.