C #: Passing derived classes as one common parameter

I recently started to learn more about events / delegates in conjunction with class extensions.

I would like to apply what I have learned into practice, adding method of expansion for the Windows Form controls called SetDraggable(), which in turn uses the event MouseDownand MouseMoveto move the control around.

Everything works fine, except for the idea that this only applies to a specific item management - in my case Button.

namespace Form_Extensions
{
    public static class Extensions
    {
        private static System.Windows.Forms.Button StubButton;
        private static Point MouseDownLocation;
        public static void SetDraggable(this System.Windows.Forms.Button b)
        {
            b.MouseDown += b_MouseDown;
            b.MouseMove += b_MouseMove;
            StubButton = b;
        }

        private static void b_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                MouseDownLocation = e.Location;
            }
        }

        static void b_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                StubButton.Left = e.X + StubButton.Left - MouseDownLocation.X;
                StubButton.Top = e.Y + StubButton.Top - MouseDownLocation.Y;
            }
        }

    }
}

As you can see, I need a special control to trigger mouse events - I cannot access these events from the parent class System.Windows.Forms.

, - , . , , System.Windows.Forms.

, , , ; , , , - , .

+4
4

System.Windows.Forms, . Control, , , :) , .

, ; SetControlDraggable :

public static void SetControlDraggable(this Control control)
{
  Point mouseDownLocation = Point.Empty;

  control.MouseDown += (s, e) =>
    {
      if (e.Button == MouseButtons.Left) mouseDownLocation = e.Location;
    }
  control.MouseUp += (s, e) =>
    {
      if (e.Button == MouseButtons.Left)
      {
        control.Left = e.X + control.Left - mouseDownLocation.X;
        control.Top = e.Y + control.Top - mouseDownLocation.Y;
      }
    }
}
+6

. Control, , Control.

public static void SetDraggable(this Control c)
{
        c.MouseDown += c_MouseDown;
        c.MouseMove += c_MouseMove;
        control = c;
}

, , sender:

private static void c_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
    Control control = (Control)sender;
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        control.Left = e.X + control.Left - MouseDownLocation.X;
        controlTop = e.Y + control.Top - MouseDownLocation.Y;
    }
}
+3

:

  public static class Extensions
{
    private static System.Windows.Forms.Control StubButton;
    private static Point MouseDownLocation;

    public static void SetControlDraggable(this System.Windows.Forms.Control b)
    {
        b.MouseDown += b_MouseDown;
        b.MouseMove += b_MouseMove;
        StubButton = b;
    }

    public static void SetDraggable<T>(this T b)
        where T:System.Windows.Forms.Control
    {
        b.MouseDown += b_MouseDown;
        b.MouseMove += b_MouseMove;
        StubButton = b;
    }

    private static void b_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            MouseDownLocation = e.Location;
        }
    }

    private static void b_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            StubButton.Left = e.X + StubButton.Left - MouseDownLocation.X;
            StubButton.Top = e.Y + StubButton.Top - MouseDownLocation.Y;
        }
    }
}
+1

, . Control, , - , , where, , form Control class.

Windows Forms , . EnableDrag , true false, .

, -, - , , , . . , getter setter extender.

DraggableExtender extender. , EnableDrag on draggableExtender1, true false . , true.

using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
[ProvideProperty("EnableDrag", typeof(Control))]
public class DraggableExtender : Component, IExtenderProvider
{
    private Dictionary<Control, bool> EnableDragValues = 
        new Dictionary<Control, bool>();
    public bool CanExtend(object extendee)
    {
        //You can limit the type of extendee here
        if (extendee is Control)
            return true;
        return false;
    }
    public bool GetEnableDrag(Control control)
    {
        if (EnableDragValues.ContainsKey(control))
            return EnableDragValues[control];
        return false;
    }
    public void SetEnableDrag(Control control, bool value)
    {
        EnableDragValues[control] = value;
        {
            if (value)
            {
                control.MouseDown += MouseDown;
                control.MouseMove += MouseMove;
                control.Cursor = Cursors.SizeAll;
            }
            else
            {
                control.MouseDown -= MouseDown;
                control.MouseMove -= MouseMove;
                control.Cursor = Cursors.Default;
            }
        }
    }

    Point p1;
    private void MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
            p1 = Cursor.Position;
    }
    private void MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            var control = (Control)sender;
            var p = control.Location;
            p.Offset(Cursor.Position.X - p1.X, Cursor.Position.Y - p1.Y);
            control.Location = p;
            p1 = Cursor.Position;
        }
    }
}

+1

Source: https://habr.com/ru/post/1672930/


All Articles