如何在运行时关闭flowlayout捕捉 [英] How do I turn off flowlayout snapping at runtime

查看:111
本文介绍了如何在运行时关闭flowlayout捕捉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个小项目,我想将标签从一个flowLayoutPanel拖到另一个。





I am creating a small project, where I want to drag a label from one flowLayoutPanel to another.


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

        int shrewsbutyTotal;
        int donningtonTotal;
        //int margin = 10;
        Point move;




        private void Form1_Load(object sender, EventArgs e)
        {
            labelBackgroundColour();
            flowLayoutPanel1.BorderStyle = BorderStyle.FixedSingle;
            flowLayoutPanel2.BorderStyle = BorderStyle.FixedSingle;
            makeMoveable();
            updateTotals();

            shrewsbutyTotal = flowLayoutPanel1.Controls.Count;
            donningtonTotal = flowLayoutPanel2.Controls.Count;
            lbl_total_shrewsbury.Text = "Shrewsbury Vehicle Count: " + shrewsbutyTotal.ToString();
            lbl_total_donnington.Text = "Donnington Vehicle Count: " + donningtonTotal.ToString();

        }
        void updateTotals()
        {
            shrewsbutyTotal = flowLayoutPanel1.Controls.Count;
            donningtonTotal = flowLayoutPanel2.Controls.Count;
            lbl_total_shrewsbury.Text = "Shrewsbury Vehicle Count: " + shrewsbutyTotal.ToString();
            lbl_total_donnington.Text = "Donnington Vehicle Count: " + donningtonTotal.ToString();
        }



        void labelMouseDown(object sender, MouseEventArgs e)
        {
            flowLayoutPanel1.Cursor = Cursors.Hand;
            move = e.Location;

        }
        void labelMouseMove(object sender, MouseEventArgs e)
        {
            Control o = (Control)sender;
            if (e.Button == MouseButtons.Left)
            {
                o.Left += e.X - move.X;
                o.Top += e.Y - move.Y;

            }
        }
        void lableMouseUp(object sender, MouseEventArgs e)
        {
            foreach (Label l in flowLayoutPanel1.Controls)
            {
                if ((flowLayoutPanel1.Location.X + l.Right) > flowLayoutPanel1.Right)
                {
                    flowLayoutPanel1.Controls.Remove(l);
                    flowLayoutPanel2.Controls.Add(l);
                    updateTotals();
                }
            }
            foreach (Label l in flowLayoutPanel2.Controls)
            {
                if ((flowLayoutPanel2.Location.X + l.Left < flowLayoutPanel2.Left))
                {
                    flowLayoutPanel2.Controls.Remove(l);
                    flowLayoutPanel1.Controls.Add(l);

                    updateTotals();
                }
            }

        }





        void labelBackgroundColour()
        {

            foreach (Label l in flowLayoutPanel1.Controls)
            {
                l.BackColor = Color.Yellow;

            }
            foreach (Label l in flowLayoutPanel2.Controls)
            {
                l.BackColor = Color.Yellow;

            }
        }
        void makeMoveable()
        {
            foreach (Label l in flowLayoutPanel1.Controls)
            {
                l.MouseDown += new MouseEventHandler(labelMouseDown);
                l.MouseMove += new MouseEventHandler(labelMouseMove);
                l.MouseUp += new MouseEventHandler(lableMouseUp);
            }
            foreach (Label l in flowLayoutPanel2.Controls)
            {
                l.MouseDown += new MouseEventHandler(labelMouseDown);
                l.MouseMove += new MouseEventHandler(labelMouseMove);
                l.MouseUp += new MouseEventHandler(lableMouseUp);
            }
        }

    }
}





我尝试了什么:



我已经能够用groupboxs和其他容器创建这个但是与这个容器混在一起。我已经添加了OnMouseUp的代码,如果标签位于容器的右侧,则将其从第一个容器中删除并将其添加到第二个容器中。但是使用flowLayoutPanels时,当用户试图移动它时,它会一直将其捕捉回布局选择。



What I have tried:

I have been able to create this with groupboxs and other containers but strugging with this one. I have put the added code that OnMouseUp if the label is on the right of the container it removes it from the first container and adds it to the second. but with flowLayoutPanels it keep snapping it back to the layout selection when the use tries to move it.

推荐答案

将此代码添加到MouseDown



add this code to MouseDown

flowLayoutPanel1.SuspendLayout();
flowLayoutPanel2.SuspendLayout();





后跟鼠标升级



followed by this in mouse up

flowLayoutPanel1.ResumeLayout();
flowLayoutPanel2.ResumeLayout();


这篇关于如何在运行时关闭flowlayout捕捉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆