How to set a default behaviour for MouseUp event in all my custom User Controls

77 Views Asked by At

I'm working on a feature that allows users to add different types of control to a panel(picturebox, label, textbox, button), also this controls should be "moveable" over the panel. To approach this, I've created(with help of other answers) a custom control class which inherits from Label, the "moveable" behavior is already implemented. I need help for abstracting all the logic in this class so I can reuse it on the other controls instead of creating one class for each control. Sample code of current CustomLabel class

public class CustomLabel : Label
{
     private bool m_Moving;
     private Cursor m_CurrentCursor;
     protected bool IsColliding { get; set;}
     private Point InitialPosition { get; set; } 

     public CustomLabel ()
        : base()
     {            
        MouseUp += CustomLabel_MouseUp;
        MouseMove += CustomLabel_MouseMove;
     }

     private void CustomLabel_MouseUp(object sender, MouseEventArgs e)
     {
        m_Moving = false;
        base.Cursor = m_CurrentCursor;
        if (this.IsColliding)
        {
            base.Location = InitialPosition;
        }
      }
      .....
}

I've also read about extension methods, but couldn't make it work with events.

0

There are 0 best solutions below