how to refresh a user control in C#

Posted on 2013-01-11
Last Modified: 2013-01-12
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_MouseDown(object sender, MouseEventArgs e)
            deltaX = this.Location.X - e.X;
            deltaY = this.Location.Y - e.Y;
            mouseIn = true;

        private void DisplayScriptVarConstCtl_MouseMove(object sender, MouseEventArgs e)
            if (mouseIn)
                this.Location = new Point(deltaX + e.X, deltaY + e.Y);


        private void DisplayScriptVarConstCtl_MouseUp(object sender, MouseEventArgs e)
            mouseIn = false;
Question by:jdcoburn
  • 2
  • 2
LVL 74

Accepted Solution

käµfm³d   👽 earned 500 total points
ID: 38769505
I think you can simplify the code to:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace _27993068
    public partial class DisplayScriptVarConstCtl : UserControl
        private int deltaX;
        private int deltaY;

        public DisplayScriptVarConstCtl()

        protected override void OnMouseDown(MouseEventArgs e)
            deltaX = (int)Math.Max(0, e.X - this.Location.X);
            deltaY = (int)Math.Max(0, e.Y - this.Location.Y);


        protected override void OnMouseMove(MouseEventArgs e)
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
                this.Location = new Point(this.Location.X + (e.X - deltaX), this.Location.Y + (e.Y - deltaY));


Open in new window


Author Closing Comment

ID: 38769906
very nicely done! i removed the delegate from the designer, so how does the system know about calling the base class (OnMouseMove)?
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 38770202
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.

Author Comment

ID: 38771099
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..

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to open/READ file from UNC path Shared Folder... 2 37
Pass through dll 2 36
String manipulation 15 49
cs.Designer Issue 2 14
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now