Dragging the mouse

Hello,

I have a control which I need to act in a specific way when the user drags the mouse on it. Means, that the user should click on the control's one position, hold the mouse button, move it to another position on the control, and releases the mouse. It isn't like drag and drop, when two objects are interacting. Here I have only one control.

Also, I need to when the user also pressed the ctrl key while dragging.

Please help.

Thanks,
Ohad.
OhadAAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vo1dCommented:
try this:
public partial class Form1 : Form
    {
        private Control controlHandle;
        private Point controlsClickPoint;
        public Form1()
        {
            InitializeComponent();                      
        }

        private void ControlMouseMove(object sender, MouseEventArgs e)
        {
            if (this.controlHandle != null)
            {                
                Point cursorPosOnForm = this.PointToClient(Cursor.Position);
                this.controlHandle.Location = new Point(cursorPosOnForm.X - this.controlsClickPoint.X,
                                                        cursorPosOnForm.Y - this.controlsClickPoint.Y);
            }
        }

        private void ControlMouseDown(object sender, MouseEventArgs e)
        {
            this.controlHandle      = sender as Control;
            this.controlsClickPoint = e.Location;
        }

        private void ControlMouseUp(object sender, MouseEventArgs e)
        {
            this.controlHandle      = null;            
        }                
    }


you have to set the eventhandlers to the events of your control.
i tested it with a button and it works.
0
OhadAAuthor Commented:
what about the control key?
0
vo1dCommented:
oh sorry, i forgot that. i will implement that in a short time.just came back from work;)
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

vo1dCommented:
here is the corrected code.
you will have to add the ControlKeyUp and ControlKeyDown event to your control too.

public partial class Form1 : Form
    {
        private Control controlHandle;
        private Point   controlsClickPoint;
        private bool    ctrlKeyPressed;
        public Form1()
        {
            InitializeComponent();            
        }

        private void ControlMouseMove(object sender, MouseEventArgs e)
        {
            if (this.controlHandle != null && this.ctrlKeyPressed)
            {                
                Point cursorPosOnForm = this.PointToClient(Cursor.Position);
                this.controlHandle.Location = new Point(cursorPosOnForm.X - this.controlsClickPoint.X,
                                                        cursorPosOnForm.Y - this.controlsClickPoint.Y);
            }
        }

        private void ControlMouseDown(object sender, MouseEventArgs e)
        {
            this.controlHandle      = sender as Control;
            this.controlsClickPoint = e.Location;
        }

        private void ControlMouseUp(object sender, MouseEventArgs e)
        {
            this.controlHandle      = null;            
        }

        private void ControlKeyDown(object sender, KeyEventArgs e)
        {
            this.ctrlKeyPressed = e.Control;
        }

        private void ControlKeyUp(object sender, KeyEventArgs e)
        {
            this.ctrlKeyPressed = false;
        }        
    }
0
OhadAAuthor Commented:
now I found that the best is using Control.ModifierKeys property
0
vo1dCommented:
oh thats fine.
0
vo1dCommented:
the solution i posted works fine.
the only thing that could be changed were the 2 eventhandler
private void ControlKeyDown(object sender, KeyEventArgs e)
        {
            this.ctrlKeyPressed = e.Control;
        }

        private void ControlKeyUp(object sender, KeyEventArgs e)
        {
            this.ctrlKeyPressed = false;
        }        
, because there was a predefined property of the control object itself.
so i dont think, that this solution should be abandoned if that means, that the solution of this problem is not browsable anymore.
regards, vo1d
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.