Click here to Skip to main content
15,878,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a main form, that have a custom control, the idea is pass the X and Y pos of the mouse when the mouse is clicked and keep in this control to main form, it's easy, but i don't know why sometimes X and Y have a negative value...

C#
public partial class NavigatorControl : UserControl
    {
        public delegate void EventHandlerNavigateControl(int x, int y);
        public event EventHandlerNavigateControl EventNavigate;
        private bool IsMouseDown = false;

        public NavigatorControl()
        {
            InitializeComponent();
        }

        void OnNavigate(int x, int y)
        {
            if (EventNavigate != null)
                EventNavigate(x, y);
        }
        
        // Here works fine, when mouse leave the control it stop to call OnNavigate...
        //private void NavigatorControl_MouseMove(object sender, MouseEventArgs e)
        //{
        //    OnNavigate(e.X, e.Y);
        //}

        private void NavigatorControl_MouseLeave(object sender, EventArgs e)
        {
            IsMouseDown = false;
        }

        private void NavigatorControl_MouseDown(object sender, MouseEventArgs e)
        {
            IsMouseDown = true;
        }

        private void NavigatorControl_MouseUp(object sender, MouseEventArgs e)
        {
            IsMouseDown = false;
        }

        private void NavigatorControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsMouseDown)
            {
                OnNavigate(e.X, e.Y);
            }
        }
    }
Posted
Updated 24-Oct-13 0:34am
v5
Comments
Kenneth Haugland 23-Oct-13 11:52am    
PRoberbly, becous you related the mouse position to your control, and csomethimes it on the outside of it...
Sergey Alexandrovich Kryukov 23-Oct-13 12:45pm    
Pretty bad code in general. Using -= is a pretty bad idea. It's the best to keep event handlers on list but have a flag to ignore the operations. Please tell me what do you want to achieve.
Besides, for a record, you code does not indicate what events are handled. This naming scheme is auto-generated, and, hence, violates good Microsoft naming conventions... For accuracy, you should always show all your initial += operators. Apparently, some events should be initially handled, otherwise none of your methods would be called. Did you check up under the debugger that everything is actually called?
—SA
Rieth L 23-Oct-13 14:56pm    
Yes all events are called. I think is wrong get negative values because it will only return values when the mouse is hover the control and it has a positive height and width
Sergey Alexandrovich Kryukov 23-Oct-13 15:25pm    
Well, a bit weird, but if it's negative, it's negative. Will you answer my questions?
—SA
Rieth L 23-Oct-13 15:30pm    
I want to take the position of mouse when i click and hold under a user control, and when the mouse go out of this user control it stop to take the position... You know DBDesignerFork or MysqlWorkbench, they have a "navigator", i want to do a thing like it

You don't share the code in MethodX, but it seems clear you want to move your Custom Control (a UserControl ?) around, and keep it within the boundaries of the Control/Form it is contained within.

There is nothing wrong with using += and -= in your code to install, and uninstall, EventHandlers !

What is questionable about what you are doing now is:

1. you are instantiating new EventHandlers in every MouseDown and MouseLeave Event; there's no need to do that (see code below). Every time you use "new MouseEventHandler(...)" you are allocating a new EventHandler object ! An interesting thing about using -= to remove an EventHandler is that it will not throw an error if the EventHandler you try to remove does not exist in the Multicast delegate that is stored in the Object's for the Event. So your code just "piles on" multiple EventHandlers !

2. you are not implementing ... that we can see ... code that actually moves the Custom Control only when the mouse is down.

The other issue here is how to keep the Custom Control inside the boundaries of its container: to do that you need to set the clipping Rectangle for the Custom Control in the MouseDown Event, and "undo" that clipping Rectangle in the MouseUp Event. We'll get to that.
C#
// You'll need some utility variables:
private bool IsMouseDown = false;
private int mouseDownX;
private int mouseDownY;

private void CustomControl_MouseDown(object sender, MouseEventArgs e)
{
    IsMouseDown = true;
    mouseDownX = e.X;
    mouseDownY = e.Y;
}

private void CustomControl_MouseUp(object sender, MouseEventArgs e)
{
    IsMouseDown = false;
}

private void CustomControl_MouseMove(object sender, MouseEventArgs e)
{
    if(IsMouseDown)
    {
        CustomControl.Left += e.X - mouseDownX;
        CustomControl.Top += e.Y - mouseDownY;
    }
}</pre>
This is all very standard code, and I would guess you are already familiar with it. If you wish to "turn on" and "turn off" the CustomControl being movable in your code you are going to need to implement something like this:
C#
private void SetCustomControlMovable(bool isMovable)
{
    if (isMovable)
    {
        CustomControl.MouseDown += CustomControl_MouseDown;
        CustomControl.MouseUp += CustomControl_MouseUp;
        CustomControl.MouseMove += CustomControl_MouseMove;
    }
    else
    {
        CustomControl.MouseDown -= CustomControl_MouseDown;
        CustomControl.MouseUp -= CustomControl_MouseUp;
        CustomControl.MouseMove -= CustomControl_MouseMove;
    }
}
So, in this case, you would not assign the EventHandlers to your CustomControl at design-time.

Now, we can address the issue of how to keep your Control constrained to move inside the boundaries of its container Control (or Form, or whatever). In this post: [^], I show how to do that.
 
Share this answer
 
v2
Comments
Rieth L 24-Oct-13 6:33am    
I have an user control called NavigatorControl, and have a FORM that have this control. I want to pass the mouse position when it is hover the control only, when it go out of this control it stop to pass the values... I update the code in answer.
Please see my comment the the question and clarify accordingly, to get further help. In the meanwhile, let me tell you one main thing about it: there is nothing wrong with having negative mouse coordinate.

—SA
 
Share this answer
 
v2

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