jdcoburn
asked on
how to refresh a user control in C#
hi - I'm using VS2010 and C#.
I have a user control on a windows form that i want to change its location. I can manage the logic to do that with the logic below. But as i drag it across the screen, it repaints at very noticable intervals, creating a "shadow" effect as it moves. i assume that it's repainting without "erasing" and every incremental move causes the new image to overlay before the old one is cancelled. how would i avoid the effect?
private void DisplayScriptVarConstCtl_M ouseDown(o bject sender, MouseEventArgs e)
{
deltaX = this.Location.X - e.X;
deltaY = this.Location.Y - e.Y;
mouseIn = true;
}
private void DisplayScriptVarConstCtl_M ouseMove(o bject sender, MouseEventArgs e)
{
if (mouseIn)
{
this.Location = new Point(deltaX + e.X, deltaY + e.Y);
this.Refresh();
}
}
private void DisplayScriptVarConstCtl_M ouseUp(obj ect sender, MouseEventArgs e)
{
mouseIn = false;
}
thanks,
Jim
I have a user control on a windows form that i want to change its location. I can manage the logic to do that with the logic below. But as i drag it across the screen, it repaints at very noticable intervals, creating a "shadow" effect as it moves. i assume that it's repainting without "erasing" and every incremental move causes the new image to overlay before the old one is cancelled. how would i avoid the effect?
private void DisplayScriptVarConstCtl_M
{
deltaX = this.Location.X - e.X;
deltaY = this.Location.Y - e.Y;
mouseIn = true;
}
private void DisplayScriptVarConstCtl_M
{
if (mouseIn)
{
this.Location = new Point(deltaX + e.X, deltaY + e.Y);
this.Refresh();
}
}
private void DisplayScriptVarConstCtl_M
{
mouseIn = false;
}
thanks,
Jim
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You're overriding the default behavior of the control in the above example. Whatever you don't override happens automatically. This is why each override calls the base version of the method--to ensure the "automatic" behavior still occurs.
One thing to note: I got the impression that you had your original code within the control's code. The "problem" with that is that event handlers are really more for things outside of the control rather than inside of it. Think about it: When you add a button to your form, where does the Click handler get generated? This is why I added the overridden methods.
One thing to note: I got the impression that you had your original code within the control's code. The "problem" with that is that event handlers are really more for things outside of the control rather than inside of it. Think about it: When you add a button to your form, where does the Click handler get generated? This is why I added the overridden methods.
ASKER
ok - thanks. I had originally had the handler outside the control's code, but that introduced other problems. and of course you're right about the override. i don't do mouse based stuff very often and it's useful to be reminded of some of the behavior..
Jim
Jim
ASKER