views:

394

answers:

2
+3  A: 

Call this.Invalidate() to get the Form/Control to redraw itself. If you know the specific region, then call one of the overloaded methods to specify what to invalidate. For example:

Rectangle toInvalidate = new Rectangle(drgargs.X - 50, drgargs.Y - 50, 50, 50);
this.Invalidate(toInvalidate);

That would invalidate an area 50 pixels around the area the drag target is.

jasonh
This didn't work. The question was specifically about doing this during a drag-n-drop event. If it works for you, there must be something I'm doing wrong...<confused>...
Greg McGuffey
Not sure why I'm being downvoted, since Invalidate does exactly what it's supposed to.
jasonh
Make sure that the UserControl has the AllowDrop property set to true. I've tested this in a simple project with a string being drawn to the control and it does change color (what I'm testing with) based on whether or not the user is dragging onto the control. Can you post an example of a project that doesn't work so I can help you get it fixed?
jasonh
Why would this get a downvote? Invalidate is the proper way to force a redraw of a control.
Ed Swangren
If it doesn't work for you than it is a problem in *your* code, not in this example.
Ed Swangren
I didn't down vote this or any answer...not enough rep. I might be my code, but the call to invalidate didn't work. Maybe it should/does in other situations, but in mine it didn't. I just commented to that effect and indicated I was aware I might be doing something wrong. Just trying to figure out the problem. I'm creating a sample app to see if I can reproduce in another environment.
Greg McGuffey
@jasonh, I did create a sample project and figured out the issue. Invalidate does indeed work (as does Refresh), but I got very confused because I was not hitting the break points within my OnPaint code where the problem was occurring and thus, incorrectly concluded that for some reason Invalidate wasn't working when doing a drag and drop. Well, I learned a ton with this one. Thanks!
Greg McGuffey
No problem. Glad you got it working!
jasonh
+2  A: 

There are three seemingly applicable methods here:

Control.Invalidate() - marks the control (region, or rectangle) as in need of repainting, but doesn't force repainting, the repaint is triggered when everything else has been taken care of and the app becomes idle.

Control.Update() - causes the control to immediately repaint if any portions have been invalidated.

Control.Refresh() - causes the control to invalidate, and then update (immediately repaint itself).

So, Refresh() is the right approach. What I would do is set a breakpoint on the refresh method call and see if/when it's being hit.

kek444
It is being hit. It executes, but doesn't actually cause OnPaint to be called.
Greg McGuffey