Solved

how to refresh a user control in C#

Posted on 2013-01-11
4
534 Views
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);
                this.Refresh();

            }
        }

        private void DisplayScriptVarConstCtl_MouseUp(object sender, MouseEventArgs e)
        {
           
            mouseIn = false;
        }
thanks,
Jim
0
Comment
Question by:jdcoburn
  • 2
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
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()
        {
            InitializeComponent();
        }

        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);

            base.OnMouseDown(e);
        }

        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));
                this.Invalidate();
            }

            base.OnMouseMove(e);
        }
    }
}

Open in new window

0
 

Author Closing Comment

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

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.
0
 

Author Comment

by:jdcoburn
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..
Jim
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

22 Experts available now in Live!

Get 1:1 Help Now