473,732 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 HighlightSectio nOfTrack(ByVal sender As Object, ByVal e
As
System.Windows. Forms.MouseEven tArgs) Handles GraphicsPB.Mous eMove

If LeftMouseButton Down Then
DragEndPoint = New Point(e.X, e.Y)
Me.Invalidate()
End If
End Sub
Protected Overrides Sub OnPaint(ByVal e As
System.Windows. Forms.PaintEven tArgs) MyBase.OnPaint( e)

If Math.Abs(DragSt artPoint.X - DragEndPoint.X) 1 And
Math.Abs(DragSt artPoint.Y - DragEndPoint.Y) 1 And
LeftMouseButton Down Then
Dim XLoc As Integer = Math.Min(DragSt artPoint.X +
GraphicsPB.Loca tion.X, DragEndPoint.X + GraphicsPB.Loca tion.X)
Dim YLoc As Integer = Math.Min(DragSt artPoint.Y +
GraphicsPB.Loca tion.Y, DragEndPoint.Y + GraphicsPB.Loca tion.Y)
e.Graphics.Draw Rectangle(Pens. Red, XLoc, YLoc,
CInt(Math.Abs(D ragStartPoint.X - DragEndPoint.X) ),
CInt(Math.Abs(D ragStartPoint.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 3611

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.Inv alidate)? 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.PaintEven tArgs)
MyBase.OnPaint( e)
Debug.WriteLine ("OnPaint")
End Sub

Private Sub Form1_Load(ByVa l sender As Object, ByVal e As
System.EventArg s) 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.EventArg s) Handles invalidateTimer .Tick
Me.Invalidate()
End Sub

Private Sub PictureBox1_Pai nt(ByVal sender As Object, ByVal e As
System.Windows. Forms.PaintEven tArgs) Handles PictureBox1.Pai nt
Debug.WriteLine ("PictureBox1_P aint")
End Sub

End Class

---snip---

On Wed, 20 Jun 2007 08:12:03 -0700, "se**@rlc.c om" <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 HighlightSectio nOfTrack(ByVal sender As Object, ByVal e
As
System.Windows .Forms.MouseEve ntArgs) Handles GraphicsPB.Mous eMove

If LeftMouseButton Down Then
DragEndPoint = New Point(e.X, e.Y)
Me.Invalidate()
End If
End Sub
Protected Overrides Sub OnPaint(ByVal e As
System.Windows .Forms.PaintEve ntArgs) MyBase.OnPaint( e)

If Math.Abs(DragSt artPoint.X - DragEndPoint.X) 1 And
Math.Abs(DragSt artPoint.Y - DragEndPoint.Y) 1 And
LeftMouseButton Down Then
Dim XLoc As Integer = Math.Min(DragSt artPoint.X +
GraphicsPB.Loc ation.X, DragEndPoint.X + GraphicsPB.Loca tion.X)
Dim YLoc As Integer = Math.Min(DragSt artPoint.Y +
GraphicsPB.Loc ation.Y, DragEndPoint.Y + GraphicsPB.Loca tion.Y)
e.Graphics.Draw Rectangle(Pens. Red, XLoc, YLoc,
CInt(Math.Abs(D ragStartPoint.X - DragEndPoint.X) ),
CInt(Math.Abs(D ragStartPoint.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
4411
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 i do? Thx a lot. Main codes go here: class frm private MyApp.Panel panel1; this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);
11
6190
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 OnPaint . The recangle draws correct when i press the mouse, but when i release the mouse the background is not restored What should i do in the Onpaint to make sure the background (the form) is restored correctly ? This problem already keeps...
8
12275
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 Invalidate(Rectangle)) flickers in a nasty way when repainting. This does not happen in the Solitare (PC + WinCe) Versions a well as in a Card game where I have the C++ Source. The use of an empty OnPaintBackground brings no visable results.
5
3084
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 controls in the collection and add them to the custom controls ControlCollection the OnPaint method is called. I would like to use my Collection property but It seems that the controls need to be added to the ControlCollection for them to work....
0
1927
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 still exists (I tried both ways). Imagine you have a Console.WriteLine( ) in the paint method so you know when its getting called. Also there is no place in the code that userControl.Paint is -+ so the event is *never* removed.
3
6193
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 why he has done something. My question is how does this work <pseudo> Function To Override OnPaint(...) Call Base Class OnPaint(...)
20
2570
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 "painting the control". It also has no effect to explicitly call txtTest.Invalidate or txtTest.Refresh. Am I missing something simple here? Any push in the right direction is appreciated.
4
4637
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 program is like so: static void Main() {
14
2970
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 reason you would want to do this is for example to trigger something from an OnPaint event without resorting to boolean switches-- say if a user presses the "M" key while the program is Painting, the user gets the PaintHandler to do something else. ...
0
9447
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9307
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.