委派超过1个事件处理程序

时间:2017-09-14 17:11:22

标签: c# delegates mouseevent eventhandler

我开发了以下代码:

namespace Main___Server
{
    public partial class Form1 : Form
    {    
        public Form1()
        {
            InitializeComponent();

            flowLayoutPanel1.AllowDrop = true;
            flowLayoutPanel2.AllowDrop = true;
            flowLayoutPanel3.AllowDrop = true;

            flowLayoutPanel1.DragEnter += panel_DragEnter;
            flowLayoutPanel2.DragEnter += panel_DragEnter;
            flowLayoutPanel3.DragEnter += panel_DragEnter;

            flowLayoutPanel1.DragDrop += panel_DragDrop;
            flowLayoutPanel2.DragDrop += panel_DragDrop;
            flowLayoutPanel3.DragDrop += panel_DragDrop;
        }

        void panel_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }

        void panel_DragDrop(object sender, DragEventArgs e)
        {
            ((GroupBox)e.Data.GetData(typeof(GroupBox))).Parent = (FlowLayoutPanel)sender;
        }

        void createOrder(string orderJSON)
        {
            JObject jsonObj = JObject.Parse(orderJSON);

            GroupBox order = new GroupBox();
            order.Size = new Size(108, 68);

            Label table = new Label();
            table.Text = "Table: " + jsonObj["table_id"];
            table.Location = new Point(2, 8);
            table.Size = new Size(50, 15);

            Label timestamp = new Label();
            timestamp.Text = DateTime.Now.ToString("HH:mm:ss");
            timestamp.Location = new Point(55, 8);
            timestamp.Size = new Size(50, 15);

            Label orderPrice = new Label();
            float totalPay = 0.0f;
            foreach (var price in jsonObj["Requests"])
            {
                totalPay += (float)price["productTotalPrice"];
            }
            orderPrice.Text = "Total price: " + totalPay.ToString() + " €";
            orderPrice.Location = new Point(2, 50);
            orderPrice.Size = new Size(100, 15);

            order.Controls.Add(table);
            order.Controls.Add(timestamp);
            order.Controls.Add(orderPrice);
            flowLayoutPanel1.Controls.Add(order);

            order.MouseDoubleClick += new MouseEventHandler(delegate (object o, MouseEventArgs a)
            {
                OrderDetails od = new OrderDetails();
                od.ShowDialog();
            });

            order.MouseDown += new MouseEventHandler(delegate (object o, MouseEventArgs a)
            {
                order.DoDragDrop(order, DragDropEffects.Move);
            });
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Simulate order 1
            Order order = new Order
            {
                table_id = 1,
                Requests = new List<requests>
                {
                    new requests {productName = "Coffee", productQuatity = 3, productTotalPrice = 1.95f},
                    new requests {productName = "Beer", productQuatity = 2, productTotalPrice = 2.20f},
                    new requests {productName = "Water", productQuatity = 1, productTotalPrice = 1f},
                    new requests {productName = "Pretzel", productQuatity = 2, productTotalPrice = 3.25f},
                }
            };

            string JSON = JsonConvert.SerializeObject(order);
            createOrder(JSON);

            //Simulate order 2
            var order2 = new Order
            {
                table_id = 1,
                Requests = new List<requests>
                {
                    new requests {productName = "Coffee", productQuatity = 3, productTotalPrice = 1.95f},
                    new requests {productName = "Pretzel", productQuatity = 2, productTotalPrice = 3.25f},
                }
            };

            string JSON2 = JsonConvert.SerializeObject(order2);
            createOrder(JSON2);
        }
    }

    public class Order
    {
        public int table_id { get; set; }
        public List<requests> Requests { get; set; }
    }

    public class requests
    {
        public string productName { get; set; }
        public int productQuatity { get; set; }
        public float productTotalPrice { get; set; }
    }
}

我得到的行为是:

  • Drag'n'drop✓
  • onClick(x)

但是,如果其中一个事件(无关紧要)被评论,另一个事件正常。

关于我做错的任何想法?

=============================================== ==========================

[溶液]

所以我设法在这里和那里得到了一些想法,并提出了这个想法:

order.MouseDown += new MouseEventHandler(delegate (object o, MouseEventArgs a)
            {
                var X = Cursor.Position.X;
                var Y = Cursor.Position.Y;

                Thread.Sleep(300);

                if ((Cursor.Position.X > X + 10 || Cursor.Position.X < X - 10) || (Cursor.Position.Y > Y + 10 || Cursor.Position.Y < Y - 10))
                    isDragAndDrop = true;
                else
                    isDragAndDrop = false;


                if ( isDragAndDrop )
                {
                    order.DoDragDrop(order, DragDropEffects.Move);
                }
                else
                {
                    OrderDetails od = new OrderDetails();
                    od.ShowDialog();
                }
            });

var isDragAndDrop是全局的并且是一个布尔值(检查用户在按下目标时是否移动了300ms。如果是,则变量变为true,启用DoDragDrop事件。)

Thread.Sleep(300)模拟“长按”。

0 个答案:

没有答案