?
Solved

Dragging the mouse

Posted on 2006-04-23
8
Medium Priority
?
267 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:OhadA
  • 5
  • 2
7 Comments
 
LVL 11

Expert Comment

by:vo1d
ID: 16519825
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
 

Author Comment

by:OhadA
ID: 16522775
what about the control key?
0
 
LVL 11

Expert Comment

by:vo1d
ID: 16527269
oh sorry, i forgot that. i will implement that in a short time.just came back from work;)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:vo1d
ID: 16527469
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
 

Author Comment

by:OhadA
ID: 16532622
now I found that the best is using Control.ModifierKeys property
0
 
LVL 11

Expert Comment

by:vo1d
ID: 16535037
oh thats fine.
0
 
LVL 11

Accepted Solution

by:
vo1d earned 2000 total points
ID: 16785118
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

809 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