Question Turn off Flowlayout Snap at runtime

bad_Radish_smell

New member
Joined
Sep 14, 2016
Messages
3
Programming Experience
Beginner
I am creating a small project, where I want to drag a label from one flowLayoutPanel to another.

C#:
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();
        }
 

 
       [COLOR=#ff0000] void labelMouseDown(object sender, MouseEventArgs e)
        {
            //somehow turn the snap feature off and then turn it back on when mouseup
            flowLayoutPanel1.Cursor = Cursors.Hand;
            move = e.Location;
 
        }[/COLOR]
        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);
            }
        }
 
    }
}


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.
 
I found the solution in the end

add this code to MouseDown

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

followed by this in mouse up


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

Latest posts

Back
Top Bottom