I have 3 data graphs that are painted via the their paint events.
When I have data that I need to insert into the graph I call the controls invalidate() command.
The first control’s paint event actually creates a bitmap buffer for the other 2 graphs to avoid repeating a long loop.
So the invalidate commands are in a specific order (1,2,3). This works well, however when the graphed data reaches the end of the graph window (PictureBox) where the data would normally start scrolling, the paint events begin firing in the wrong order (2,3,1).
has anyone came across this before? why might this be happening?
Change your code so that before calling
Invalidateon any of the three controls, you create the one shared bitmap buffer (conceivably as a static member of your control class), and then callInvalidateon each of the controls. Within the control’sPaintevent you can then use the static bitmap buffer, and it won’t matter in which order thePaintevents fire.When you call
Invalidateon a control, you’re basically telling the OS to send a WM_PAINT message to that control. Because it’s a Windows message, it’s guaranteed to be delivered whenever Windows gets around to doing it. In your case, they’re usually delivered in the order received, but sometimes they just won’t be.One other thing to consider with your code: when you place relatively complex drawing code inside your control’s
Paintevent handler (or inside a method called directly from thePaintevent handler), this code will execute whenever the control is invalidated for any reason, meaning that the code will run when you callInvalidate, but it will also run whenever another window is dragged over the control.For complex, time-consuming graphics, it’s always best to perform the complex rendering on a hidden buffer (a
Bitmapor an invisiblePictureBoxor whatever), and then in the control’sPaintevent do a simple copy from the hidden buffer to the visible window (usingGraphics.DrawImageorBitBltor whatever).This approach also allows you to avoid flicker, if you add a second buffer in between the buffer you draw on and the visible window (hence “double buffering”). After you complete drawing on the main buffer, you copy it onto the second buffer. In the control’s
Paintevent, you copy from the second buffer onto the visible window.