Save a flowlayoutpanel controls (IE labels) when a user closes application

bad_Radish_smell

New member
Joined
Sep 14, 2016
Messages
3
Programming Experience
Beginner
I need to save the location of the labels when the user closes, i think i need to make it serialisable or save something to an XML file but cannot figure out how to do it.

the Labels can be moved from one FlowLayoutPanel to another by the use of the if statement and then add/remove methods.

C#:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Xml;
using System.IO;
using System.Xml.Serialization;
using System.Data.SqlClient;





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();

            

        }
        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)
        {
            Disableflowsnap();
            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)
        {

            flowLayoutPanel1.Cursor = Cursors.Default;
            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();
                }
            }
            EnableFlowSnap();


        }





        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);
            }
        }
        void Disableflowsnap()
        {
            foreach (FlowLayoutPanel flp in Controls.OfType<FlowLayoutPanel>())
            {
                flp.SuspendLayout();
            }

        }
        void EnableFlowSnap()
        {
            foreach (FlowLayoutPanel flp in Controls.OfType<FlowLayoutPanel>())
            {
                flp.ResumeLayout();
            }

        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            foreach (FlowLayoutPanel test in Controls.OfType<FlowLayoutPanel>();
            {
                //in here i believe i need the write to xml code
            }
            
        }

        private void btn_load_Click(object sender, EventArgs e)
        {

        }
    }
}
 
It would seem logical to have two StringCollection items in the application settings. You can then save the Text of each Label into one or the other, depending on which FlowLayoutPanel they are in. At startup, you simply loop through the two StringCollections and add a Label to the appropriate FlowLayoutPanel for each item.
 

Latest posts

Back
Top Bottom