当用户关闭应用程序时,保存FlowLayoutPanel控件(即标签)

bad_radish_smell.

新成员
加入
2016年9月14日
消息
3
编程经验
Beginner
当用户关闭时,我需要保存标签的位置,我想我需要使其将其Serialisable或保存到XML文件中,但无法弄清楚如何执行此操作。

通过使用IF语句然后添加/删除方法,可以将标签从一个流程移动到另一个流程。

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)
        {

        }
    }
}
 

jmplhinney.

C#论坛主持人
工作人员
加入
2011年4月23日
消息
3,732
地点
悉尼,澳大利亚
编程经验
10+
在应用程序设置中有两个StringCollection项目似乎是逻辑的。然后,您可以将每个标签的文本保存到一个或另一个中,具体取决于它们所在的流程图。在启动时,您只需循环到两个StringCollections并为每个项目添加标签到适当的FlowLayoutPanel。
 
最佳 底部