Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hello Everyone,
I am stuck with yet another small problem.

Situation: :doh:
There is a panel, and if MouseDown event occurs on the panel(myPanel), a user control (myShape) will be added to the controls of panel on the location where the Mouse Button was down. Now i need to perform some task inside the MouseMove event of newly added Control myShape.

Problem: :((
Even though the added control myShape shows up on the place of mouse pointer on mouseDown event, but moving the mouse (without unpressing the mouse button) over myShape doesnot trigger the myShape's MouseMove Event.
However unpressing the button, and then moving the mouse triggers the MouseMove Event.

Help required: :confused:
Is there a way to trigger the MouseMove event of the newly added control, without unpressing the Mouse Button.

simplified code is as below:

C#
public class myForm : form
{
    private void Intialize()
    {
        this.Controls.Add(this.myPanel);
    }
    private void myPanel_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button != MouseButtons.Left)
            return;
        MyShape myShape = new MyShape();
        listOfShape.Add(myShape);
        this.Controls.Add(myShape);
        myShape.Location = e.Location;
        myShape.BringToFront();
    }
}

public class MyShape : UserControl
{
    // I need to jump to this function without UnPressing the mouse button //
    private void MyShape_MouseMove(object sender, MouseEventArgs e)
    {
        // Do some Task //
    }
}


Thanks already.
Posted
Updated 20-Sep-10 20:10pm
v3
Comments
Per Söderlund 21-Sep-10 2:19am    
Good question and easy to understand your problem.
Abhishrek 21-Sep-10 3:37am    
hi Soderlund ... what about the answer ?

1 solution

The issue is:

The MouseMove Event is being called, however, its the mouse move event on the Panel! not on the shape!

The best way to fix this problem is simply to handle the MouseMove Event of the panel and move the control like this:

note: you have to make myShape variable have class scope, rather than method scope.

private void myShape_MouseMove(object sender, MouseEventArgs e)
{
  myShape.Location = e.Location();
}


also, you have to protect yourself because MouseMove is going to get called a lot and more importantly BEFORE the control may be on the panel, so you need to write some code to make sure the MouseMove does nothing until the control is actually been put on the form. One possibility is just to use a flag to be false and make it change to true in the MouseDown event. Make sure you protect against threading issues by using a lock.

Dave
 
Share this answer
 
v2
Comments
Abhishrek 21-Sep-10 1:41am    
Thanks for the quick reply David.
Although,
"The best way to fix this problem is simply to handle the MouseMove Event of the panel."
I disagree with it.
I already tried that, but once the MouseButton is released and then mouse is moved over the control, Control's MouseMove event will be triggered and i need to write the similar task inside the control's MouseMove event handler also.
On the other hand, there will be many such contorls are to be added in the panel on MouseDown, it will be troublesome to identify and select particular task for a particular control.
I'd like to go for the OOP approach and prefer to write the task inside each Control's MouseMove Event handler.

And thanks for the advise for checking about the control in MouseMove event handler. I will keep that in mind.
DavidKiryazi 21-Sep-10 2:10am    
Interesting thoughts. I look forward to seeing hopefully a better response than the one I posted :)
Abhishrek 27-Sep-10 9:54am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
Abhishrek 27-Sep-10 10:05am    
Hi David,
I solved the issue but in a little unorthodox way.

In the mouseMove event handler of the panel, I manipulated mouse coordinates of mouseEventArgs as if it was moving inside a control (myShape), and then called the mouseMove Event of myShape inside this event handler, passing new mouseEventArgs. It worked fine in my case, but this approach may not be helpful in every case.

Thanks for your reply though.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900