1

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.

gorkem
  • 731
  • 1
  • 10
  • 17
4D1C70
  • 490
  • 3
  • 10
  • Since the base class is different for your different controls you can not reuse the code using inheritance. As an option, you can create an extender component to solve the problem. Here is an [example](http://stackoverflow.com/a/42957116/3110834). The `DraggableExtender` component in the example lets you to make all kinds of control draggable. Also here is [another example](http://stackoverflow.com/a/39952406/3110834) which makes the control movable and sizable using a helper class. – Reza Aghaei May 12 '17 at 22:07
  • @Reza Aghaei thanks for the extension properties examples, it helped me a lot. – 4D1C70 May 13 '17 at 23:51

0 Answers0