I would like trigger the drag target control to redraw itself (via either invalidate or refresh) during the DragEnter and DragLeave events. The code looks something like:
protected override void OnDragEnter (DragEventArgs drgargs)
{
//-- Set a property that affects drawing of control, then redraw
this.MyProperty = true;
this.Refresh(); //-- Does nothing???
}
protected override void OnDragLeave (EventArgs e)
{
//-- Set a property that affects drawing of control, then redraw
this.MyProperty = false;
this.Refresh(); //-- Does nothing???
}
It doesn't actually redraw the control, the OnPaint method is not called by the Refresh() within these events. Is there any way to do this? I must not be understanding something here.
UPDATE: the answer provided by jasonh doesn't actually work. When using Invalidate() or Invalidate(rect) the control does not actually update. This is being calls during a drag and drop action. Any other ideas? Can you trigger a redraw of a control during a drag and drop? Thanks!
UPDATE 2: I created a sample project and could not get this to not work. Sigh... I finally tracked it down to some code in the OnPaint that was causing the problem. So, this turned out to be more of me not understanding how the debugger worked (it never hit break points in the OnPaint...still don't know why). Invalidate(), Refresh() both work. JasonH gets the answer as it was ultimately correct and also showed how to invalidate just a portion of a control...I didn't know about that.
Thanks for all your help!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With